Skip to content

Instantly share code, notes, and snippets.

@raindev
Created November 15, 2014 18:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raindev/d3b310341018fdbda35d to your computer and use it in GitHub Desktop.
Save raindev/d3b310341018fdbda35d to your computer and use it in GitHub Desktop.
Game of life produced on the last coding session of Kyiv Global Day of Coderetreat 2014
import Data.List
-- Transform cells into next iteration
nextTurn :: (Integral a) => [(a,a)] -> [(a,a)]
nextTurn space = [(x, y) | (x,y,_) <- [head xs | xs <- cells space, length xs == 3 || (length xs == 4 && True `elem` ([x | (_,_,x) <- xs]))]]
-- Cells grouped by their coordinates
cells :: (Integral a) => [(a,a)] -> [[(a,a,Bool)]]
cells space = groupBy (\(x1,y1,_) (x2,y2,_) -> x1 == x2 && y1 == y2) (sort (neighbourhood space))
-- List of alive cells and their neighbours
neighbourhood :: (Integral a) => [(a,a)] -> [(a,a,Bool)]
neighbourhood space = [ (x, y, True) | (x, y) <- space ] ++ neighbours space
-- List of neighbour cells marked as dead
neighbours :: (Integral a) => [(a,a)] -> [(a,a,Bool)]
neighbours space = [ (x+dx, y+dy, False) | (x,y) <- space, dx <- [-1..1], dy <- [-1..1], dx /= 0 || dy /= 0 ]
@ffbit
Copy link

ffbit commented Nov 18, 2014

Where are the unit tests?

@raindev
Copy link
Author

raindev commented Nov 25, 2014

The solution was developed interactively (and iteratively) using GHCi.

Honestly, I have very little experience with Haskell and don't know how to write unit tests yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment