Monad Imparative Usage Example

Monad Imparative Usage Example Thanks for your replies. I have not haskell on this computer and I will try this solutions tonight. I must notice that IO computations is not the point here. My target is to have this code for mutable variable 'var'. ############################################################################# Haskell is the most powerfull and interesting "thing" I'v ever encountered in IT world. But with an imparative background and lack of understanding (because of any thing include that maybe I am not that smart) has brought me problems. I know this is an old issue. But please help it. Question : Could anyone show me a sample of using a monad as a statefull variable? For example see this code in C# : // public class Test { int var; static void Fun1() { var = 0; Console.Write(var); } static void Fun2() { var = var + 4; Console.Write(var); } static void Main() { Fun1(); Fun2(); var = 10; Console.Write("var = " + var.ToString()); } } // I want to see this code in haskell. Thankyou #############################################################################

kaveh.shahbazian:
Monad Imparative Usage Example
Thanks for your replies. I have not haskell on this computer and I will try this solutions tonight. I must notice that IO computations is not the point here. My target is to have this code for mutable variable 'var'.
Still not entirely clear what your goal in the translation is. The most "Haskell" way would be to emulate a mutable variable with a state monad, but you seem to want an actual named mutable variable? So here's an example, like Seb's, where we create a mutable variable (in the IO monad) and mutate it all over the place. This uses similar scoping to your original code. For fun we use MVars instead of IORefs. import Control.Concurrent.MVar main = do var <- newEmptyMVar let write = withMVar var print f1 = do putMVar var 0 write f2 = do modifyMVar_ var (return.(+4)) write f1 f2 swapMVar var 10 write Produces: $ runhaskell A.hs 0 4 10 Of course, if you're learning Haskell, you should probably try to /avoid/ mutable variables for a while. -- Don

On Wed, 2 Aug 2006, Donald Bruce Stewart wrote: ...
Of course, if you're learning Haskell, you should probably try to /avoid/ mutable variables for a while.
Along the same line, I note that proposed solutions seem to use features relatively recently added to the language, is that true? StateT requires multi-parameter type class, for example, so it can't have been there all along. MVar is pretty new, isn't it? IORef must be the oldest of them, but hardly there from the start, I suspect. To learn core concepts, maybe it's a good idea to stay away from GHC in the beginning, and use Hugs or something that tends not to be so much of a magnet for new features. That forces you to look for a solution on the terms of the basic language concepts. Donn

Thanks All
This is about my tries to understand monads and handling state - as
you perfectly know - is one of them. I have understood a little about
monads but that knowledge does not satidfy me. Again Thankyou
On 8/2/06, Donn Cave
On Wed, 2 Aug 2006, Donald Bruce Stewart wrote: ...
Of course, if you're learning Haskell, you should probably try to /avoid/ mutable variables for a while.
Along the same line, I note that proposed solutions seem to use features relatively recently added to the language, is that true? StateT requires multi-parameter type class, for example, so it can't have been there all along. MVar is pretty new, isn't it? IORef must be the oldest of them, but hardly there from the start, I suspect.
To learn core concepts, maybe it's a good idea to stay away from GHC in the beginning, and use Hugs or something that tends not to be so much of a magnet for new features. That forces you to look for a solution on the terms of the basic language concepts.
Donn
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Donn Cave
-
dons@cse.unsw.edu.au
-
Kaveh Shahbazian