Compare commits
2 Commits
ef3b7511e8
...
5e655f67f3
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e655f67f3 | |||
| 123f880c86 |
31
problems/cons_car_cdr.py
Normal file
31
problems/cons_car_cdr.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
"""
|
||||||
|
cons(a, b) constructs a pair, and car(pair) and cdr(pair) return the first
|
||||||
|
and last element of that pair.
|
||||||
|
|
||||||
|
car(cons(3, 4)) == 3
|
||||||
|
cdr(cons(3, 4)) == 4
|
||||||
|
|
||||||
|
Given this implementation of cons, implement car and cdr.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def cons(a, b):
|
||||||
|
return lambda f: f(a, b)
|
||||||
|
|
||||||
|
|
||||||
|
def car(pair):
|
||||||
|
return pair(lambda a, b: a)
|
||||||
|
|
||||||
|
|
||||||
|
def cdr(pair):
|
||||||
|
return pair(lambda a, b: b)
|
||||||
|
|
||||||
|
|
||||||
|
# --- tests ---
|
||||||
|
|
||||||
|
assert car(cons(3, 4)) == 3
|
||||||
|
assert cdr(cons(3, 4)) == 4
|
||||||
|
assert car(cons("a", "b")) == "a"
|
||||||
|
assert cdr(cons("a", "b")) == "b"
|
||||||
|
|
||||||
|
print("All tests passed!")
|
||||||
BIN
resources/SystemDesignInterview.pdf
Normal file
BIN
resources/SystemDesignInterview.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user