Skip to content

Commit

Permalink
add 3.22
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph williamon15 committed Oct 3, 2018
1 parent abdf7c4 commit 3f79490
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 3.22.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#lang sicp

(define (make-queue)
(let ((front-ptr '())
(rear-ptr '()))
(define print
(begin
(display front-ptr)
(newline)))
(define empty?
(null? front-ptr))
(define (insert item)
(let ((new-pair (cons item '())))
(cond (empty?
(set! front-ptr new-pair)
(set! rear-ptr new-pair))
(else (set-cdr! rear-ptr new-pair)
(set! rear-ptr new-pair)))))
(define (dispatch m)
(cond ((eq? m 'print) print)
((eq? m 'empty?) empty?)
((eq? m 'insert) insert)
(else (error "make-queue: undefined operation" m))))
dispatch))

(define (print-queue q) (q 'print))
(define (empty-queue? q) (q 'empty?))
(define (insert-queue q i) ((q 'insert) i))

(define q1 (make-queue))
(print-queue q1)
(empty-queue? q1)
(print-queue q1)
59 changes: 59 additions & 0 deletions 3.22.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#lang sicp

(define (make-queue)
(let ((front-ptr '())
(rear-ptr '()))
(define (print)
(begin
(display front-ptr)
(newline)))
(define (empty?)
(null? front-ptr))
(define (front)
(if (empty?) (error "front-queue on empty queue")
(car front-ptr)))
(define (delete)
(cond ((empty?) (error "delete-queue on empty queue"))
(else (set! front-ptr (cdr front-ptr))
(print))))
(define (insert item)
(let ((new-pair (cons item '())))
(cond ((empty?)
(set! front-ptr new-pair)
(set! rear-ptr new-pair))
(else (set-cdr! rear-ptr new-pair)
(set! rear-ptr new-pair)
(print)))))
(define (dispatch m)
(cond ((eq? m 'print) (print))
((eq? m 'empty?) (empty?))
((eq? m 'insert) insert)
((eq? m 'front) (front))
((eq? m 'delete) (delete))
(else (error "make-queue: undefined operation" m))))
dispatch))

(define (print-queue q) (q 'print))
(define (empty-queue? q) (q 'empty?))
(define (insert-queue q i) ((q 'insert) i))
(define (front-queue q) (q 'front))
(define (delete-queue q) (q 'delete))

(define q1 (make-queue))
(print-queue q1)
(empty-queue? q1)
(insert-queue q1 'a)
(front-queue q1)
(empty-queue? q1)
(insert-queue q1 'b)
(insert-queue q1 'c)
(front-queue q1)
(print-queue q1)
(delete-queue q1)
(front-queue q1)
(print-queue q1)
(delete-queue q1)
(front-queue q1)
(print-queue q1)
(delete-queue q1)
(empty-queue? q1)

0 comments on commit 3f79490

Please sign in to comment.