Last active
September 1, 2019 19:47
-
-
Save michal-stoma/fbd4558ca47aafa504164b94e4ce0f8d to your computer and use it in GitHub Desktop.
Daily Coding Problem #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' Daily Coding Problem #5 | |
| This problem was asked by Jane Street. | |
| cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. | |
| Given this implementation of cons: | |
| def cons(a, b): | |
| def pair(f): | |
| return f(a, b) | |
| return pair | |
| Implement car and cdr. | |
| ''' | |
| def cons(a, b): | |
| def pair(f): | |
| return f(a, b) | |
| return pair | |
| def car(f): | |
| def left(a, b): | |
| return a | |
| return f(left) | |
| def cdr(f): | |
| def right(a, b): | |
| return b | |
| return f(right) | |
| # alternatively with lamba: | |
| # def cdr(f) | |
| # return f(lambda a, b: b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment