-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.js
208 lines (158 loc) · 3.86 KB
/
lambda.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** Lambda calculus */
// example values
const a = 'a'
const b = 'b'
const c = 'c'
const T = 'T'
const F = 'F'
// anonymous identity
console.log(
((x) => x)(a)
) // a
// multiple arguments
var f1 = (x) => x
var f2 = (x) => x
console.log(
((x, y, z) => x(y(z)))(f1, f2, c)
) // c
// named identity
const identity = (x) => x
console.log(
identity(a)
) // a
const apply = (f, x) => f(x)
const identity2 = (x) => apply(identity, x)
console.log(
identity2(a)
) // a
// numbers
const _0 = (f, x) => x
const succ = (n) => (f, x) => f(n(f, x))
const _1 = succ(_0)
const _2 = succ(_1)
const _3 = succ(_2)
const _4 = succ(_3)
const _5 = succ(_4)
const add = (m, n) => (f, x) => m(f, n(f, x))
const n = (x) => x + 1
console.log(
_0(n, 0), // 0
succ(_0)(n, 0), // 1
succ(_1)(n, 0), // 2
succ(succ(_0))(n, 0), // 2
succ(_2)(n, 0), // 3
succ(succ(succ(_0)))(n, 0), // 3
add(_1, _2)(n, 0), // 3
add(_3, _2)(n, 0), // 5
)
// conditional branching
const first = (x, y) => x
const second = (x, y) => y
console.log(
first(a, b), // a
second(a, b) // b
)
const True = first
const False = second
const not = (x) => x(False, True)
console.log(
not(True)(T, F), // F
not(False)(T, F) // T
)
const and = (x, y) => x(y, False)
console.log(
and(True, False)(T, F), // F
and(False, True)(T, F), // F
and(True, True)(T, F), // T
and(False, False)(T, F), // F
)
const or = (x, y) => x(True, y)
console.log(
or(True, False)(T, F), // T
or(False, True)(T, F), // T
or(True, True)(T, F), // T
or(False, False)(T, F), // F
)
const ifElse = (x, t, f) => x(t, f)
console.log(
ifElse(True, a, b), // a
ifElse(False, a, b), // b
ifElse(or(False, True), a, b), // a
ifElse(and(False, True), a, b), // b
ifElse(or(and(False, True), or(False, True)), a, b), // a
)
// loops
const succ2 = (x) => x + 1 // numeric functions
const pred = (x) => x - 1
const isZero = (x) => x === 0
const addBase1 = (f, x, y) => isZero(y) ? x : f(f, succ2(x), pred(y))
const add1 = (x, y) => addBase1(addBase1, x, y)
const recursive = (f) => ((s) => f(s(s)))((s) => f(s(s)))
// const addBase2 = (f, x, y) => ifElse(isZero(y), x, f(succ2(x), pred(y))) // Maximum call stack size exceeded
const addBase2 = (f, x, y) => isZero(y) ? x : f(succ2(x), pred(y))
const add2 = (x, y) => recursive(addBase2)(x, y)
console.log(
add1(2, 1), // 3
// add2(2, 1) // Maximum call stack size exceeded
)
// data types
const tuple = (a, b) => (f) => f(a, b)
const t42 = tuple(4, 2)
console.log(
t42(first), // 4
t42(second) // 2
)
const list = (h, t) => tuple(h, t)
const head = (list) => list(first)
const tail = (list) => list(second)
const nil = list(0, 0)
const l1 = list(1, nil)
const l2 = list(1, list(2, nil))
const l3 = list(1, list(2, list(3, nil)))
console.log(
head(l3), // 1
tail(l3)(first), // 2
tail(l3)(second)(first) // 3
)
const isEmpty = (list) => isZero(head(list))
console.log(
isEmpty(l1), // false
isEmpty(l2), // false
isEmpty(l3), // false
isEmpty(nil), // true
)
const lengthBase = (f) => (list) => isEmpty(list) ? 0 : succ2(f(f)(tail(list)))
const length = lengthBase(lengthBase)
console.log(
length(l1), // 1
length(l2), // 2
length(l3), // 3
length(nil), // 0
)
const selectBase = (f) => (list, n) => isZero(n) ? head(list) : f(f)(tail(list), pred(n))
const select = selectBase(selectBase)
console.log(
select(l1, 0), // 1
select(l2, 0), // 1
select(l2, 1), // 2
select(l3, 0), // 1
select(l3, 1), // 2
select(l3, 2), // 3
)
// functional vs imperative
var x1 = 1
var y1 = x1
x1 = x1 + 1
console.log('x', x1, 'y', y1)
var x2 = 1
x2 = x2 + 1
var y2 = x2
console.log('x', x1, 'y', y2)
const x3 = 1
const y3 = x3
const z3 = apply(succ2, x3)
console.log('x', x3, 'y', y3)
// halting problem
console.log(
((s) => s(s))((s) => s(s))
)