3 ms·
There also is the classical example of defining cons, car, and cdr as closures in SICP: (define (cons x y) (lambda (m) (m x y))) (define (car z) (
by Someone 6y ago
There also is the classical example of defining cons, car, and cdr as closures in SICP:
(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)))
Here, cons, instead of returning an object with two values, returns a closure that captures the two values. car and cdr call that closure, passing it another closure. The cons closure calls that closure, passing the the two values.
Explanation at https://stackoverflow.com/questions/21769348/use-of-lambda-for-cons-car-cdr-definition-in-sicp https://stackoverflow.com/questions/21769348/use-of-lambda-f....
- chriswarbo 6y agohttps://en.wikipedia.org/wiki/Church_encoding https://en.wikipedia.org/wiki/Church_encoding
- Joker_vD 6y agoAlso, https://en.wikipedia.org/wiki/Scott_encoding https://en.wikipedia.org/wiki/Scott_encoding Of course, it completely kills (or "stress-tests", if you will) the branch predictor in your CPU.
- avmich 6y agoI like this explanation - https://stevelosh.com/blog/2013/03/list-out-of-lambda/ https://stevelosh.com/blog/2013/03/list-out-of-lambda/ .