[Haskell-cafe] Very fast loops. Now!

Henning Thielemann lemming at henning-thielemann.de
Sun Feb 18 15:18:56 EST 2007


On Sun, 11 Feb 2007, 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)

No one doubts, that it is possible to write efficient code in Haskell. But
how does idiomatic Haskell code perform?


More information about the Haskell-Cafe mailing list