diff --git a/3.22.bak b/3.22.bak new file mode 100644 index 0000000..2ec215b --- /dev/null +++ b/3.22.bak @@ -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) \ No newline at end of file diff --git a/3.22.rkt b/3.22.rkt new file mode 100644 index 0000000..4b9532f --- /dev/null +++ b/3.22.rkt @@ -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) \ No newline at end of file