-
Notifications
You must be signed in to change notification settings - Fork 0
/
Life.purs
45 lines (39 loc) · 941 Bytes
/
Life.purs
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
module Life where
import Prelude
import Data.Grid (Grid(..))
import Data.Grid as Grid
import Data.Array as Array
import Data.Foldable (foldr, foldl)
import Control.Comonad (extract, extend)
type Life = Grid Boolean
aliveNeighbours :: Life -> Int
aliveNeighbours life = f $ extract <$> Grid.neighbours life
where
f = Array.length <<< Array.filter (eq true)
rule :: Life -> Boolean
rule life =
case aliveNeighbours life of
2 -> extract life
3 -> true
_ -> false
evolve :: Life -> Life
evolve = extend rule
glider :: Life
glider =
Grid.init false 25 25
# Grid.right
# Grid.down
# Grid.right
# Grid.write true
# Grid.down
# Grid.right
# Grid.write true
# Grid.down
# Grid.write true
# Grid.left
# Grid.write true
# Grid.left
# Grid.write true
# Grid.up
# Grid.up
# Grid.up