[Haskell-cafe] Project Euler Problem 357 in Haskell
Daniel Fischer
daniel.is.fischer at googlemail.com
Tue Nov 8 15:18:03 CET 2011
On Tuesday 08 November 2011, 14:54:18, Silvio Frischknecht wrote:
> On 11/08/2011 02:19 PM, Ryan Yates wrote:
> > If I compile with optimizations:
> >
> > ghc --make -O3 primes.hs
So far, -O3 is not different from -O2 (-On gives you -O2 for n > 2).
*Never* compile code you want to use without optimisations.
Compiling without optimisations is strictly for development, when
compilation time matters because of frequent recompilation.
Once things have stabilised, compile them with optimisations only.
> >
> > I get an answer that is off by one from the C++ program in a few
> > seconds.
>
> nice one. Though i wonder. The problem seems to be that without
> optimization sum is not tail-recursive. Is sum meant to not be
> tail-recursive?
Well, it is tail recursive (foldl, basically), but not strict.
So without optimisations you get the worst of boths worlds (tail recursion
means no incremental output, as could be possible with foldr for lazy
number types, not being strict in the accumulator means it builds huge
thunks, like foldr with a function strict in the second argument).
>
> ghci> sum [1..]
>
> eats up all the memory within seconds while
>
> ghci> foldl' (+) 0 [1..]
>
> does not
>
> So Mukesh if you want your program to run without -Ox you should
> probably define your one sum'
>
> import Data.List
> sum' = foldl' (+) 0
That'd help, it would still be dog-slow, though, since optimisation is also
crucial for the sieve.
>
> Silvio
More information about the Haskell-Cafe
mailing list