[Haskell-cafe] Buttons and Clicks - State Monad

2009-01-31 Thread guenni68
Hi,

in this piece here http://moonpatio.com/fastcgi/hpaste.fcgi/view?id=1083#a1083
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?

Günther

The UI Code is wxHaskell
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Buttons and Clicks - State Monad

2009-01-31 Thread Cale Gibbard
You might be misunderstanding the purpose of the State Int monad somewhat.

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
state.

 - Cale

2009/1/31 guenni68 red...@fedoms.com:
 Hi,

 in this piece here http://moonpatio.com/fastcgi/hpaste.fcgi/view?id=1083#a1083
 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?

 Günther

 The UI Code is wxHaskell
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Buttons and Clicks - State Monad

2009-01-31 Thread Henk-Jan van Tuyl


Note that, for wxHaskell, you should use Var instead of IORef, to be  
future proof.


Regards,
Henk-Jan van Tuyl


--
http://functor.bamikanarie.com
http://Van.Tuyl.eu/
--


On Sat, 31 Jan 2009 19:38:41 +0100, Cale Gibbard cgibb...@gmail.com  
wrote:


You might be misunderstanding the purpose of the State Int monad  
somewhat.


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
state.

 - Cale

2009/1/31 guenni68 red...@fedoms.com:

Hi,

in this piece here  
http://moonpatio.com/fastcgi/hpaste.fcgi/view?id=1083#a1083

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?

Günther

The UI Code is wxHaskell
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe





--

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe