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

Kurt Hutchinson kelanslists at gmail.com
Thu Feb 2 14:08:58 EST 2006


On 2/2/06, Maurício <briqueabraque at yahoo.com> wrote:
>    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
> };

An idiomatic approach:
example :: Double -> Double -> IO ()
example a b
    | a == b    = return ()
    | otherwise = do
        let a' = a / 2
        print a'
        b' <- readLn
        example a' b'

main = example 1000 0


More information about the Haskell-Cafe mailing list