I can't seem to explain how this definition of cons, car and cdr works:
(define (p-cons x y)
(lambda (proc) (proc x y)))
(define (p-car proc)
(proc (lambda (p q) p)))
(define (p-cdr proc)
(proc (lambda (p q) q)))
I understand that the first procedure returns a procedure which takes a procedure as an argument, and applies this procedure to the two arguments x y. What I don't understand is how car and cdr works:
(p-car (p-cons "foo" "bar")) -> foo
(p-cdr (p-cons "foo "bar")) -> bar
If someone could explain how this works I would appreciate it! How does the lambda-expression in 'car' and 'cdr' get the 'p' and 'q' variables for exemple?