-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conway.hs
66 lines (54 loc) · 2.48 KB
/
Conway.hs
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
{-# LANGUAGE FlexibleContexts #-}
module Conway where
import Control.Comonad
import Control.Monad
import Data.Default
import Data.Foldable (foldr')
import ListZipper
import Plane
import Universe
instance Default Bool where
def = False
neighbours :: Universe a b => [a b -> a b]
neighbours = [left, right, up, down,
upleft, upright, downleft, downright]
aliveNeighbours :: (Universe a Bool, Comonad a) => a Bool -> Int
aliveNeighbours z = foldr' count 0 neighbours
where
count d c = if extract . d $ z then c + 1 else c
conway :: (Universe a Bool, Comonad a) => a Bool -> Bool
conway z =
case aliveNeighbours z of
2 -> extract z
3 -> True
_ -> False
evolve :: (Universe a Bool, Comonad a) => a Bool -> a Bool
evolve = extend conway
glider :: Matrix a Bool => a Bool
glider = fromMatrix m
where
m = [ [f, t, f]
, [f, f, t]
, [t, t, t] ]
t = True
f = False
gliderGun :: Matrix a Bool => a Bool
gliderGun = fromMatrix m
where
m = [ [f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, t, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, t, f, t, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, f, t, t, f, f, f, f, f, f, t, t, f, f, f, f, f, f, f, f, f, f, f, f, t, t, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, t, f, f, f, t, f, f, f, f, t, t, f, f, f, f, f, f, f, f, f, f, f, f, t, t, f]
, [f, t, t, f, f, f, f, f, f, f, f, t, f, f, f, f, f, t, f, f, f, t, t, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, t, t, f, f, f, f, f, f, f, f, t, f, f, f, t, f, t, t, f, f, f, f, t, f, t, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, t, f, f, f, f, f, t, f, f, f, f, f, f, f, t, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, t, f, f, f, t, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, f, t, t, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f]
, [f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f] ]
t = True
f = False
space :: Matrix a Bool => Int -> Int -> a Bool
space x y = fromMatrix m
where
m = take y . repeat $ take x . repeat $ False