Created
September 8, 2020 19:55
-
-
Save emgullufsen/0d651add5bc397ab6ae3c96a861ec797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; SICP Exercise 3.22 | |
;; Queue structure implemented as a procedure with local state | |
;; Rik G. - September 2020 | |
(define (make-queue) | |
(let ((front-ptr '()) | |
(rear-ptr '())) | |
(define (dispatch m) | |
(cond ((eq? m 'insert) | |
(lambda (z) | |
(let ((new-pair (cons z '()))) | |
(if (null? front-ptr) | |
(begin | |
(set! front-ptr new-pair) | |
(set! rear-ptr new-pair) | |
(cons front-ptr rear-ptr) | |
) | |
(begin | |
(set-cdr! rear-ptr new-pair) | |
(set! rear-ptr new-pair) | |
(cons front-ptr rear-ptr) | |
) | |
) | |
) | |
) | |
) | |
((eq? m 'delete) | |
(lambda () | |
(if (null? front-ptr) | |
(error "delete on empty") | |
(begin (set! front-ptr (cdr front-ptr)) (cons front-ptr rear-ptr)) | |
))) | |
(else (error "whatup")) | |
) | |
) | |
dispatch)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment