Skip to content

Instantly share code, notes, and snippets.

@emgullufsen
Created September 8, 2020 19:55
Show Gist options
  • Save emgullufsen/0d651add5bc397ab6ae3c96a861ec797 to your computer and use it in GitHub Desktop.
Save emgullufsen/0d651add5bc397ab6ae3c96a861ec797 to your computer and use it in GitHub Desktop.
;; 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