#35: All CARs and CDRs

Tagged as challenge

Written on 2018-02-06

Most Lisps have combinations of car and cdr built in, usually up to four levels deep, e.g., caaddr and cdaddr are built-ins referring to

(lambda (x) (car (car (cdr (cdr x)))))
; and
(lambda (x) (cdr (car (cdr (cdr x)))))

respectively. Implement a macro (generate-cxr n) which generates the definitions of all car/cdr combinations up to n times.

If your language doesn't support macros or dynamic definitions, write a function of the same name which produces a list of all car/cdr combinations as anonymous functions.

Extra Credit: Implement this as efficiently as possible, using the least amount of space.

Unless otherwise credited all material copyright © 2010–2018 by Robert Smith