Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Created June 23, 2016 12:24
Show Gist options
  • Save svanellewee/846731a1c031f73b8c118b30fda1c5ba to your computer and use it in GitHub Desktop.
Save svanellewee/846731a1c031f73b8c118b30fda1c5ba to your computer and use it in GitHub Desktop.
Cons in Python
def cons(a, b):
return lambda fn : fn(a, b)
a_list = cons("Shane", cons("Monster", cons("Maniac", None)))
print a_list
def printy(a,b):
if b:
return "({} {})".format(a, b(printy))
else:
return "({}. nil)".format(a)
print a_list(printy)
def car(a,b):
return a
def cdr(a,b):
return b
print ">>", a_list(car)
print ">>", a_list(cdr)(printy)
print ">>", a_list(cdr)(cdr)(printy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment