Created
November 15, 2014 18:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] |
Actually, the case when we have alive cell with two neighbours falls into the first part of the quoted condition (1A + 2D), as well as newborn cell. Second part threat specially alive cell with three neighbours (1A + 3D).
I've tested it one more time on Toad oscillator, it looks like the thing is working.
Where are the unit tests?
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
Why
length xs == 3 || (length xs == 4 && True
elem([x | (_,_,x) <- xs]))
?If I understand correctly it should contains:
length xs == 2 && True
elem`