Created
June 23, 2016 12:24
-
-
Save svanellewee/846731a1c031f73b8c118b30fda1c5ba to your computer and use it in GitHub Desktop.
Cons in Python
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
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