Skip to content

Instantly share code, notes, and snippets.

@vijayanant
Created June 4, 2021 15:47
Show Gist options
  • Save vijayanant/8a730bd9d897c2c46c3eb052062df3b3 to your computer and use it in GitHub Desktop.
Save vijayanant/8a730bd9d897c2c46c3eb052062df3b3 to your computer and use it in GitHub Desktop.
Simple Python problem #1 --- pair, car, and cdr
# Problem Statement
# 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 the below implementation for cons( ), please implement car & cdr
def cons(a, b):
def pair(f):
return f(a, b)
return pair
def car (f) :
return f(lambda x, y : x)
def cdr (f) :
return f(lambda x, y : y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment