I think you're looking for IORef http://www.haskell.org/ghc/docs/ latest/html/libraries/base/Data-IORef.html

Something like this (untested) should do what you want:

example :: IO ()
example = do { ref <- newIORef 1000; loop ref }
 where loop ref = do
       x <- readIORef ref
       print x
       when (x>1) (writeIORef ref (x/2) >> loop ref)


On Feb 2, 2006, at 10:57 AM, Maurício wrote:

Donald Bruce Stewart wrote:
briqueabraque:
 Hi,

I would like to know what options I have in Haskell to do something similar to this C++ code:

double a = 1000;
while (a>1) a/=2;

I'm able to do that with lists, but I would like to know how to do that with monads and variables with state.
You'll get good code using a normal recusive loop:
    main = print (loop 1000)
        where
loop a | a <= 1 = a | otherwise = loop (a/2)
All such control structures may be implemented using recursion.
-- Don

I understand those examples, but I really would like to know how to do that with monads. I would like to ask the same question, but now with this code:

double a = 1000;
double b = 0;
while (a != b) {
    a /= 2;
    cout << a; // Prints a
    cin << b; // User gives a number, stored in b
};

  Best,
  Maurício


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



Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
          -- TMBG



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

Reply via email to