[Haskell-cafe] Haskell code for this example of flow control

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Feb 1 20:00:26 EST 2006

>   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)
            loop a | a <= 1    = a 
                   | otherwise = loop (a/2)

All such control structures may be implemented using recursion.

-- Don

More information about the Haskell-Cafe mailing list