-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_v0.py
More file actions
30 lines (23 loc) · 1.11 KB
/
Copy pathpython_v0.py
File metadata and controls
30 lines (23 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
zero = lambda _, x: x
one = lambda f, x: f(x)
two = lambda f, x: f(f(x))
three = lambda f, x: f(f(f(x)))
four = lambda f, x: f(f(f(f(x))))
five = lambda f, x: f(f(f(f(f(x)))))
to_python_int = lambda n: n(lambda x: x + 1, 0)
true = lambda t, _: t
false = lambda _, f: f
pred_nested = lambda x, next: lambda _: lambda c: c(x, next)
pred_step = lambda f: (lambda g: g(g))(lambda s: lambda acc: lambda c: c(f(acc), lambda _: s(s)(f(acc)) ))
pred = lambda num: lambda f, x: num(lambda v: v(x)(false), pred_nested(x, pred_nested(x, pred_step(f))) )(x)(true)
sub = lambda first, second: lambda f, x: first(lambda v: v(x)(false), second(lambda next: pred_nested(x, next), pred_nested(x, pred_step(f))) )(x)(true)
assert(to_python_int(pred(zero)) == 0)
assert(to_python_int(pred(one)) == 0)
assert(to_python_int(pred(two)) == 1)
assert(to_python_int(pred(three)) == 2)
assert(to_python_int(pred(five)) == 4)
assert(to_python_int(sub(four, three)) == 1)
assert(to_python_int(sub(three, one)) == 2)
assert(to_python_int(sub(three, two)) == 1)
assert(to_python_int(sub(one, three)) == 0)
assert(to_python_int(sub(one, five)) == 0)