Skip to content

Instantly share code, notes, and snippets.

@gatlin
Last active December 30, 2015 10:59
Show Gist options
  • Save gatlin/7820016 to your computer and use it in GitHub Desktop.
Save gatlin/7820016 to your computer and use it in GitHub Desktop.
DIY cons cell with car/cdr accessors using closures in Scheme (R5RS)
;; lovingly stolen from SICP
;; http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_sec_2.1.3
(define (my-cons x y)
(lambda (m) (m x y)))
(define (my-car z)
(z (lambda (p q) p)))
(define (my-cdr z)
(z (lambda (p q) q)))
;; tests
(define a (my-cons 2 '()))
(define b (my-cons 1 a))
(display
(list
(my-car a)
(my-car b)
(my-cdr a)
(my-cdr b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment