[Haskell-cafe] Re: Buttons and Clicks - State Monad
redcom at fedoms.com
Sat Jan 31 14:03:39 EST 2009
thanks for the explanation. From my futile attempts I *almost* gathered as
I guess I was getting confused with documentation saying things like
put replaces the state inside the monad.
that this would refer to some sort of update-in-place.
So bottom-line then there is no other way than using IORef.
Am 31.01.2009, 19:38 Uhr, schrieb Cale Gibbard <cgibbard at gmail.com>:
> You might be misunderstanding the purpose of the State Int monad
> A computation of type State Int a is internally represented by a
> function of type Int -> (Int, a). When you call runState, you
> effectively apply this pure function to an initial state, and get a
> final state and result. You won't be able to do anything with the
> State monad that you couldn't already do with such a function, it's
> more or less a notational convenience.
> In particular, the state of your counter will not be preserved between
> calls to runState unless you arrange that the final state returned
> from the last call to runState is passed along as the initial state in
> the next one. Of course, this effectively defeats the purpose of using
> the State monad in the first place.
> Since event handlers in wxHaskell must be in the IO monad, there's no
> machinery in place to handle forwarding your state along, so the State
> monad is not terribly useful here. On the other hand, it's rather easy
> to write a function of type State s a -> IORef s -> IO a which takes
> the initial state from the IORef and updates the IORef with the new
> - Cale
> 2009/1/31 guenni68 <redcom at fedoms.com>:
>> in this piece here
>> I'm trying to create a button that, every time when clicked, increases
>> a counter by one and does a putStrLn of the counters current value.
>> I'm trying to write this without any use of IORef but merely using the
>> state monad.
>> Can anybody show me how to do this?
>> The UI Code is wxHaskell
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
More information about the Haskell-Cafe