3 ms·
Also, cons, car, and cdr need not be primitives. They can be built out of lambdas. In Scheme: (define (cons x y) (lambda (m) (m x y))) (define (car z)
by Someone 8y ago
Also, cons, car, and cdr need not be primitives. They can be built out of lambdas. In Scheme:
(define (cons x y)
(lambda (m) (m x y)))
(define (car z)
(z (lambda (p q) p)))
(define (cdr z)
(z (lambda (p q) q)))
(From https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-14.html#%25_sec_2.1.3 https://mitpress.mit.edu/sites/default/files/sicp/full-text/..., exercise 2.4)
- kazinator 8y agoSure. Now to implement consp, all you need is a global list which tracks all lambdas that came from the cons function; if the argument object is eq to anything on that list, then it is a cons! We also now need another primitive: weak pointers. We don't want our global list to prevent otherwise unreferenced cells from being reclaimed. typecase is going to be fun to develop and optimize, with everything being a lambda.