Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

<< [05] Implement Lisp's car and cdr given cons >>

cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair.

Given this implementation of cons below, implement car and cdr.

>>> def cons(a, b):
...     def pair(f):
...         return f(a, b)
...     return pair

>>> car, cdr = coding_problem_05()
>>> car(cons('first', 'last')) == 'first'
True

>>> cdr(cons('first', 'last')) == 'last'
True