![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
This is a somewhat odd cellular automaton implementation in Haskell. It encodes the shape of the world, and the stepping rule, in the data structure. Briefly:
data Cell v n = Cell { value :: v, neighbors :: n (Cell v n), future :: Cell v n }
-- Build a cell and its chain of futures, given its rule, similar neighbors, -- and initial value. mkCell :: Functor n => (v -> n v -> v) -> v -> n (Cell v n) -> Cell v n mkCell rule val nei = initial where initial = Cell val nei (calcFuture initial) calcFuture c = c' where c' = Cell (rule (value c) (fmap value (neighbors c))) (fmap future (neighbors c)) (calcFuture c') data Moore c = Moore c c c c c c c c -- Conway's Life evalLife :: Bool -> Moore Bool -> Bool evalLife v n = f v (sumN fromEnum n) where f _ 3 = True f True 2 = True f _ _ = False
The remainder of the code is routines for setting up worlds and displaying them.
- GraphLife.hs — The core and text-based display
- GraphLifeGL.hs — OpenGL display
Nice
Date: 2007-08-29 14:59 (UTC)Just when I think I know haskell, I see something like this and am reminded that I am not Enlightened.
If you get time, could you post a brief explanation of how it works?
Re: Nice
Date: 2007-09-01 17:09 (UTC)That's it, is it clearer for you with this ? (Or have I just made it even more foggy ?)
--
Jedaï
(no subject)
Date: 2008-10-14 00:37 (UTC)(no subject)
Date: 2008-10-14 01:16 (UTC)Of course, it's indistinguishable from any other un-fancified Life implementation, but hey.