-
Notifications
You must be signed in to change notification settings - Fork 78
/
life.fe
112 lines (94 loc) · 1.63 KB
/
life.fe
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
(= nth (fn (n lst)
(while (< 0 n)
(= n (- n 1))
(= lst (cdr lst)))
(if (is n 0) (car lst))
))
(= rev (fn (lst)
(let res nil)
(while lst
(= res (cons (car lst) res))
(= lst (cdr lst))
)
res
))
(= map (fn (f lst)
(let res nil)
(while lst
(= res (cons (f (car lst)) res))
(= lst (cdr lst))
)
(rev res)
))
(= print-grid (fn (grid)
(map
(fn (row)
(print (map (fn (x) (if (is x 0) '- '#)) row))
)
grid
)
))
(= get-cell (fn (grid x y)
(or (nth x (nth y grid)) 0)
))
(= next-cell (fn (grid cell x y)
(let n (+
(get-cell grid (- x 1) (- y 1))
(get-cell grid (- x 1) y )
(get-cell grid (- x 1) (+ y 1))
(get-cell grid x (- y 1))
(get-cell grid x (+ y 1))
(get-cell grid (+ x 1) (- y 1))
(get-cell grid (+ x 1) y )
(get-cell grid (+ x 1) (+ y 1))
))
(if
(and (is cell 1) (or (is n 2) (is n 3))) 1
(and (is cell 0) (is n 3)) 1
0
)
))
(= next-grid (fn (grid)
(let y -1)
(map
(fn (row)
(= y (+ y 1))
(let x -1)
(map
(fn (cell)
(= x (+ x 1))
(next-cell grid cell x y)
)
row
)
)
grid
)
))
(= life (fn (n grid)
(let i 1)
(while (<= i n)
(print ">> iteration" i)
(print-grid grid)
(print)
(= grid (next-grid grid))
(= i (+ i 1))
)
))
; blinker in a 3x3 universe
(life 5 '(
(0 1 0)
(0 1 0)
(0 1 0)
))
; glider in an 8x8 universe
(life 22 '(
(0 0 1 0 0 0 0 0)
(0 0 0 1 0 0 0 0)
(0 1 1 1 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
))