Data as Procedures with No Data

Further to my post on data as procedures, I’ve translated to Common Lisp the implementation of cons, car and cdr as procedures with no data, not even numbers.

This comes from lecture 5b of the Structure and Interpretation of Computer Programs video series, with the necessary (funcall) adjustments for Common Lisp.

(defun my-cons (x y)
    (lambda (m) (funcall m x y)))

(defun my-car (x)
  (funcall x (lambda (a d) a)))

(defun my-cdr (x)
  (funcall x (lambda (a d) d)))

(print (my-car (my-cons 35 47)))
(print (my-cdr (my-cons 77 22)))

Output is:

35
22

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.