Last active
February 7, 2022 21:06
-
-
Save p7g/cd3410cbd565065064b1baac6b618063 to your computer and use it in GitHub Desktop.
i have invented the LISt Processor
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
import operator | |
nil = () | |
def nilp(l): | |
return l == nil | |
def cons(a, b): | |
return a, b | |
def car(l): | |
a, b = l | |
return a | |
def cdr(l): | |
a, b = l | |
return b | |
def caar(l): | |
(a, b), c = l | |
return a | |
def cadr(l): | |
(a, b), c = l | |
return b | |
def cdar(l): | |
a, (b, c) = l | |
return b | |
def cddr(l): | |
a, (b, c) = l | |
return c | |
def lmap(l, f): | |
return nil if nilp(l) else cons(f(car(l)), lmap(cdr(l), f)) | |
def fold(l, f): | |
def fold_(l, f, init): | |
return init if nilp(l) else fold_(cdr(l), f, f(init, car(l))) | |
return fold_(cdr(l), f, car(l)) | |
l = cons(1, cons(2, cons(3, cons(4, cons(5, nil))))) | |
print(fold(lmap(l, lambda n: 2 * n), operator.add)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment