-
Notifications
You must be signed in to change notification settings - Fork 2
/
rule110.snow
79 lines (68 loc) · 1.88 KB
/
rule110.snow
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
-- create a grid of size
createGrid size
: Int -> Array<Bool>
= if 0 < (size - 1)
then (push (createGrid (size - 1)) false)
else [false]
isAlive a b c
: String -> String -> String -> Bool
= if a and b and c -- 111
then false -- 0
else if a and b and !c -- 110
then true -- 1
else if a and !b and c -- 101
then true -- 1
else if a and !b and !c -- 100
then false -- 0
else if !a and b and c -- 011
then true -- 1
else if !a and b and !c -- 010
then true -- 1
else if !a and !b and c -- 001
then true -- 1
else false -- 000
-- 0
idxOf i len
: Int -> Int -> Int
= (i + len) mod len
nextCell grid x
: Array<Bool> -> Int -> Bool
= (λlen ->
(λa ->
(λb ->
(λc -> isAlive a b c)
(nth grid (idxOf (x + 1) len)))
(nth grid x ))
(nth grid (idxOf (x - 1) len)))
(length grid)
displayCell cell
: Bool -> String
= if cell then "#" else " "
nextGen grid idx
: Array<Bool> -> Int -> Array<Bool>
= if idx < (length grid)
then (push nextGen (idx + 1))
else [nextCell grid idx]
joinStr sep arr
: String -> Array<Bool> -> String
= if (length arr) > 0
then (sep + (head arr)) + (joinStr sep (tail arr))
else ""
map f arr
- : (a -> b) -> Array<a> -> Array<b>
= if length arr == 0
then []
else [f (head arr)] + map f (tail arr)
display grid
: Array<Bool> -> IO
= head [grid, (print (joinStr "" (map displayCell grid)) "\n")]
run count grid
: Array<Bool> -> Array<Bool>
= run (count + 1) (nextGen (display grid) 0)
-- main
-- : IO
-- = (\starting_grid ->
-- (\grid -> run 0 grid)
-- (display starting_grid))
-- (push (createGrid 9) true)
main = print (map (λx -> x + 1) [1, 2, 3])