Skip to content

Instantly share code, notes, and snippets.

@folksilva
Last active June 19, 2018 21:29
Show Gist options
  • Save folksilva/d024ade4e2b6870f258df87d148a3c15 to your computer and use it in GitHub Desktop.
Save folksilva/d024ade4e2b6870f258df87d148a3c15 to your computer and use it in GitHub Desktop.
Daily Coding Problem: 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):
return lambda f : f(a, b)
Implement car and cdr.
https://dailycodingproblem.com/
"""
def cons(a, b):
return lambda f: f(a, b)
def car(f):
return f(lambda a, b: a)
def cdr(f):
return f(lambda a, b: b)
if __name__ == '__main__':
# Read input of two numbers, separated by commas
a, b = (int(n) for n in input().split(','))
print(car(cons(a, b)))
print(cdr(cons(a, b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment