-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.scm
161 lines (135 loc) · 3.41 KB
/
lambda.scm
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
;true
(define true (lambda (x) (lambda (y)
(eval x (interaction-environment)))))
;false
(define false (lambda (x) (lambda (y)
(eval y (interaction-environment)))))
;not: (nott true) -> false
(define nott
(lambda (x)
((x false) true)))
;and: ((andd true) false) -> false
(define andd
(lambda (x) (lambda (y)
((x y) false))))
;or: ((orr true) false) -> true
(define orr
(lambda (x) (lambda (y)
((x true) y))))
;xor: ((xor true) false) -> true
(define xor
(lambda (x) (lambda (y)
((x (nott y)) y))))
;if: (((if true) one) zero) -> one
(define iff
(lambda (x) (lambda (y) (lambda (z)
((x y) z)))))
;zero
(define zero
(lambda (f) (lambda (x) x)))
;one
(define one
(lambda (f) (lambda (x) (f x))))
;two
(define two
(lambda (f) (lambda (x) (f (f x)))))
;three
(define three
(lambda (f) (lambda (x) (f (f (f x))))))
;four
(define four
(lambda (f) (lambda (x) (f (f (f (f x)))))))
;five
(define five
(lambda (f) (lambda (x) (f (f (f (f (f x))))))))
;addition: ((two ++) one) -> 2+1 = 3
(define ++
(lambda (y) (lambda (f)
(lambda (x) (f ((y f) x))))))
;multiplication: ((** one) two) -> 1*2 = 2
(define **
(lambda (x) (lambda (y)
(lambda (f) (x (y f))))))
;exponentional: ((^ two) three -> 2^3 = 8
(define ^
(lambda (n) (lambda (m)
(m n))))
;zero?
(define iszero?
(lambda (x) (((x false) nott) false)))
;make a pair: ((pair two) one) -> [two, one]
(define pair
(lambda (x) (lambda (y)
(lambda (z) ((z x) y)))))
;car: (head ((pair two) one)) -> two
(define head
(lambda (p) (p true)))
;cdr: (tail ((pair two) one)) -> one
(define tail
(lambda (p) (p false)))
;even?: (iseven? two) -> true
(define iseven?
(lambda (x) ((x nott) true)))
;(n, n2 -> n+1, n)
(define phi
(lambda (p)
(lambda (z) ((z (++ (p true))) (p true)))))
;predecessor: ((one --) two) -> 2-1 = 1
(define --
(lambda (n)
(((n phi) ((pair zero) zero)) false)))
;greater-than-or-equal?: ((ge? two) one) -> true
(define ge?
(lambda (x) (lambda (y)
(iszero? ((x --) y)))))
;equal?: ((e? two) two) -> true
(define e?
(lambda (x) (lambda (y)
((andd ((ge? x) y))
((ge? y) x)))))
;greater?: ((g two) one) -> true, ((g? two) two) -> false
(define g?
(lambda (x) (lambda (y)
(nott ((ge? y) x)))))
;remainder: ((rem four) three) -> one
(define rem
(lambda (n) (lambda (m)
(((iff ((ge? n) m))
`((rem ((,m --) ,n)) ,m))
n))))
;division: ((// four) three) -> 4/3 = 1
(define //
(lambda (numo) (lambda (deno)
(((iff ((ge? numo) deno))
`((one ++) ((// ((,deno --) ,numo)) ,deno)))
zero))))
;add all integers to n: (sum n) -> 1 + 2 + .. + (n-1) + n ie. n*(n+1)/2
(define sum
(lambda (n)
(((iff (iszero? n))
zero)
`((,n ++) (sum (-- ,n))))))
;factorial: (factorial four) -> 4*3*2*1 = 24
(define factorial
(lambda (n)
(((iff (iszero? n))
one)
`((** ,n) (factorial (-- ,n))))))
;fizz-buzz: 1=fizz, 2=buzz, 3=fizz-buzz: (fizz-buzz five) -> 2
(define fizz-buzz
(lambda (n)
(((((iff (iszero? ((rem n) three)))
one)
zero)
++)
(((iff (iszero? ((rem n) five)))
two)
zero))))
;for debugging: (show-number four) -> I I I I
(define show-number
(lambda (x) ((x (lambda (x) (display "I "))) (lambda (f) (lambda (x) x)))
(display "\n")))
;for debugging: (lam-num 3) -> three
(define lam-num
(lambda (x)
((one ++) (if (> x 1) (lam-num (- x 1)) zero))))