[Haskell-beginners] State Monads (and OpenGL)

Tom Hobbs tvhobbs at googlemail.com
Fri Nov 5 11:55:08 EDT 2010

Hi all,

I have a program which implements Conway's Game Of Life.  It even has
an open GL UI which shockingly works.

I've followed a tutorial
which tells how to get the open GL to animate, and according to my
sysouts, that's working to.  The problem I have comes with the fact
that between every frame I want to evolve my world but I'm really
struggling working out how to get the state stuff working.

Here's a snippet of my main function;

main = do
	world <- sequence (take (worldSize * worldSize) $ repeat rand) --
randomly generate a square world
	-- code snipped out
	displayCallback $= (display position world)  -- display method which
is called on every frame redraw

Here, world is a [Int] and represents my cells.  The display function
as referenced by "displayCallback" is the function that is called back
to everytime the frame is redrawn.

I also have another function which takes in a world, updates the state
of the cells and the evolved world, thus;

evolve :: [Int] -> [Int]

I've been reading through (a lot of) tutorials on the state monad but
I'm not any closer to working out how I make the call to display take
some kind of updated world.

I've also got the following code;

type World = State [Int]

evolveS :: World [Int]
evolveS = do
              w <- get
              let w2 = evolve w
              put w2
              return w2

Which I believe to be the correct code for wrapping my [Int] in the
state monad but now I have two problems;

1) I don't know if it's correct
2) I don't know how to apply it, even if it is

Can anyone offer any advice?

Many thanks,


More information about the Beginners mailing list