Skip to content

Instantly share code, notes, and snippets.

@michal-stoma
Last active September 1, 2019 19:47
Show Gist options
  • Select an option

  • Save michal-stoma/fbd4558ca47aafa504164b94e4ce0f8d to your computer and use it in GitHub Desktop.

Select an option

Save michal-stoma/fbd4558ca47aafa504164b94e4ce0f8d to your computer and use it in GitHub Desktop.
Daily Coding Problem #5
''' 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