[Haskell-cafe] Sometimes I wish there was a global variable

Rafael Cunha de Almeida almeidaraf at gmail.com
Sat Mar 21 20:29:21 EDT 2009


Hello,

I am writing a OpenGL program in haskell, it can be found in:
	http://github.com/aflag/galo/tree/master
But I hope this e-mail will be self-contained :).

My main function goes like this:
    (...)
    rotX <- newIORef (0.0::GLfloat)
    rotY <- newIORef (0.0::GLfloat)
    pos <- newIORef (0.0::GLfloat, 0.0, 0.0)

    displayCallback $= display (map f range) rotX rotY pos

    keyboardMouseCallback $= Just (keyboardMouse rotX rotY pos)
    (...)

Notice that rotX, rotY and pos are meant to be used as comunication
between the keyboardMouse and display functions. They need to be set as
0 first, so display won't do anything. Only when they user press a few
buttons that those values change, so display behaves accordanly.

In a state-based language I would place display and keyboardMouse in one
module and let them communcate to each other like they want. In haskell,
I'm not quite sure how to do it except by that parameter passing style.

I thought about how state-monad may help with that. But I'm not sure how
I'd make the state variable to be contained inside a
display/keyboardMouse module.

[]'s
Rafael


More information about the Haskell-Cafe mailing list