Donald Bruce Stewart wrote:
The following C program was described on #haskell

    #include <stdio.h>

    int main()
    {
        double x = 1.0/3.0;
        double y = 3.0;
        int i    = 1;
        for (; i<=1000000000; i++) {
            x = x*y/3.0;
            y = x*9.0;
        }
        printf("%f\n", x+y);
    }


Which was translated to the following Haskell:

    {-# OPTIONS -fexcess-precision #-}

    import Text.Printf

    main = go (1/3) 3 1

    go :: Double -> Double -> Int -> IO ()
    go !x !y !i
        | i == 1000000000 = printf "%f\n" (x+y)
        | otherwise       = go (x*y/3) (x*9) (i+1)


Do the two programs implement the same algorithm? The C program updates x and y in sequence. The Haskell program updates x and y in parallel and can be easier for the compiler to optimize.

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to