[Haskell] how to write an interactive program ? gui library to use ?

Chris Kuklewicz haskell at list.mightyreason.com
Sat Feb 25 14:27:13 EST 2006

Dean Herington wrote:
> At 7:31 PM +0100 2/24/06, minh thu wrote:
>> Hi all,
>> 1/
>> I'd like to know how can I implement an interactive program (an
>> editor) in haskell where some "things" have to be updated.
>> The "things" can be text in a word processor, or a pixel array in a 2d
>> graphics editor, and so on.
>> Have I to pass the state (the "things") explicitely in the arguments
>> of the main loop (a recursive function) ?
>> Sorry if it's stupid...
> No, it's not a stupid question at all...
> Essentially, the answer is "yes", the state needs to be passed around
> (neglecting hackery to simulate global variables that is better
> avoided).  However, this can be made convenient by using a monad. You
> define a monad which is based on IO and carries your state (something
> like StateT YourState IO a; see
> http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State.html).

If the things need to be more explicitly mutable you can
*) Create a bunch of IORefs (or MVar for thread safety)
*) Put them into some data structure (your own or perhaps tuples)
*) Use a ReaderT from Control.Monad.Reader, ReaderT YourData IO a

Then you can
*) lookup the IORef you need with using "asks"
*) read/write/update that mutable data

More information about the Haskell mailing list