[Haskell-cafe] mixing wxhaskell state and file io

Martin DeMello martindemello at gmail.com
Mon Feb 5 02:37:54 EST 2007

On 2/5/07, Matthew Brecknell <haskell at brecknell.org> wrote:
> I'm not familiar with wxHaskell, but I don't think wxHaskell is your
> problem here. It looks like you are confusing yourself with overuse of
> "do" notation, and perhaps a lack of understanding of the monad laws.
> Whenever you see this:
> v <- expr
> return v
> You can replace it with just "expr". Your code above reduces to:
> words <- varCreate (readWords "words")

Okay - was confused about that. Thanks for the tip.

> So the variable you are creating has type (IO WordMap), when you
> probably wanted it to have type WordMap. By itself, this compiles,
> because IO actions are values as good as any other. The problem only
> manifests when you combine it with code that tries to read the variable
> as if it had type WordMap.
> This is probably what you wanted (untested):
> w <- readWords "words"
> words <- varCreate w

That didn't work, because (as someone on #haskell explained to me)
readWords has type IO and the wx actions have type Layout

> The wxHaskell Var type is not quite like the State monad. It is a type
> of mutable variable in the IO monad, much like the IORef type. Try to
> avoid gratuitous use of mutable variables, as they can weaken your
> ability to reason about your code. I'm not convinced you really need one
> here. What problem are you trying to solve by using a mutable variable?

You're right, I don't need mutable variables, just some sort of state.
I'm writing a game which presents the user with a list of anagrams
(read in from a file) and an input box in which to enter guesses. The
input box's on command is (pseudocode)

a <- get input text
if exists a wordmap
  remove a wordmap
  update display
  say "wrong"

So what I need is for the gui code to have a single wordmap threaded
through it. I thought I could do that by reading in the wordmap within
the gui do sequence and binding it to a variable.


More information about the Haskell-Cafe mailing list