Skip to content

Instantly share code, notes, and snippets.

@mattyw
Forked from nschmoller/gol.hs
Created December 3, 2011 15:40
Show Gist options
  • Save mattyw/1427401 to your computer and use it in GitHub Desktop.
Save mattyw/1427401 to your computer and use it in GitHub Desktop.
Game of life in Haskell (in 45 minutes)
zeroes 0 = []
zeroes n = [0] ++ (zeroes (n-1))
foo x y z= zipWith (+) (zipWith (+) x y) z
grid_neighbours l =
let top = map inc_neighbours ( [(zeroes (length (head l)))] ++ (take (length l) l) )
bottom = map inc_neighbours ((drop 1 l) ++ [(zeroes (length (head l)))])
current = map neighbours l
in zipWith3 foo top bottom current
inc_neighbours l = zipWith (+) (neighbours l) l
neighbours l =
let right = (drop 1 l) ++ [0]
left = [0] ++ (take (length l) l)
in zipWith (+) left right
next_state cs nr
| cs == 1 && (nr == 2 || nr == 3 )= 1
| cs == 0 && nr == 3 = 1
| otherwise = 0
make_step l= zipWith make_row_step l (grid_neighbours l)
make_row_step l r = zipWith next_state l r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment