-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
215 lines (170 loc) · 3.95 KB
/
index.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
209
210
211
212
213
214
// Seed values as a list of x,y pairs: ( ( x, y), (x, y) )
//
// blinker
// var next = cons(cons(num1, num2), cons(cons(num2, num2), cons(cons(num3, num2), nil)))
// toad
// var next = cons(cons(num1, num2), cons(cons(num2, num2), cons(cons(num3, num2), cons(cons(num2, num3), cons(cons(num3, num3), cons(cons(num4, num3), nil))))))
// glider
var next = cons(cons(num3, num1), cons(cons(num4, num2), cons(cons(num2, num3), cons(cons(num3, num3), cons(cons(num4, num3), nil)))))
display(next)
setInterval(function () {
next = start(next)
display(next)
}, 1000)
function start (last) {
return filter(listNeighbors(last), last, nil, nil)
}
function filter (neighbors, last, living, seen) {
return lif(nul(neighbors), makeConst(living), function (cell) {
return lif(member(cell, seen), skip, function () {
return (function (tot) {
return lif(eq(tot,num3), live, function () {
return lif(eq(tot,num2), function () {
return lif(member(cell, last), live, die)()
}, die)()
})()
})(howMany(neighbors, cell, num0))
})()
function live () {
return filter(cdr(neighbors), last, cons(cell, living), cons(cell, seen))
}
function die () {
return filter(cdr(neighbors), last, living, cons(cell, seen))
}
function skip () {
return filter(cdr(neighbors), last, living, seen)
}
})(car(neighbors))
}
function member (cell, list) {
return lif(eq(howMany(list, cell, num0), num0), f, t)
}
function howMany (list, cell, tot) {
return lif(nul(list), makeConst(tot), function () {
return howMany(cdr(list), cell, lif(samePoint(car(list), cell), inc(tot), tot))
})()
}
function samePoint (a, b) {
return lif(and(eq(car(a), car(b)), eq(cdr(a), cdr(b))), t, f)
}
function listNeighbors (l) {
return _listNeighbors(l, nil)
}
function _listNeighbors (l, sofar) {
return lif(nul(l), makeConst(sofar), function (x, y) {
return _listNeighbors(cdr(l),
cons(cons(inc(x), y),
cons(cons(x, inc(y)),
cons(cons(inc(x), inc(y)),
cons(cons(inc(x), dec(y)),
cons(cons(dec(x), inc(y)),
cons(cons(dec(x), y),
cons(cons(x, dec(y)),
cons(cons(dec(x), dec(y)),
sofar)))))))))
})(car(car(l)), cdr(car(l)))
}
// lists
function cons (a, b) {
return function (op) {
return op(a, b)
}
}
function car (a) {
return a(t)
}
function cdr (a) {
return a(f)
}
function nil () {
return t
}
function nul (p) {
return p(makeConst(f))
}
// logic
function t (a, b) {
return a
}
function f (a, b) {
return b
}
function lif (a, b, c) {
return a(b, c)
}
function and (a, b) {
return a(b, f)
}
// maths
function eq (a, b) {
return lif(and(isZero(sub(a, b)), isZero(sub(b, a))), t, f)
}
function inc (n) {
return function (a) {
return function (b) {
return a(n(a)(b))
}
}
}
function isZero (n) {
return n(makeConst(f))(t)
}
function dec (n) {
return car(n(shift)(cons(num0, num0)))
}
function shift (x) {
return cons(cdr(x), inc(cdr(x)))
}
function sub (a, b) {
return b(dec)(a)
}
function add (a, b) {
return b(inc)(a)
}
// util
function id (x) {
return x
}
function makeConst (x) {
return function (_) {
return x
}
}
// num literals for seed values
function num0 (_) {
return id
}
function num1 (fn) {
return function (x) {
return fn(x)
}
}
function num2 (fn) {
return function (x) {
return fn(fn(x))
}
}
function num3 (fn) {
return function (x) {
return fn(fn(fn(x)))
}
}
function num4 (fn) {
return function (x) {
return fn(fn(fn(fn(x))))
}
}
// display logic
function display (l) {
console.log(_display(l, '( '))
}
function _display (l, sofar) {
return lif(nul(l), function () {
return sofar + ')'
}, function () {
return _display(cdr(l), sofar + '( ' + num(car(car(l))) + ', ' + num(cdr(car(l))) + ' ) ')
})()
}
function num (n) {
return n(function (x) { return x + 1 })(0)
}