[Haskell-cafe] First time haskell - parse error!

Daniel Fischer daniel.is.fischer at web.de
Wed Mar 10 09:18:34 EST 2010


Am Mittwoch 10 März 2010 14:53:32 schrieb Stephen Tetley:
> Hello all
>
> Algorithmically oddSquareSum is a bit below par though...
>
> > oddSquareSum :: Integer
> > oddSquareSum = sum . takeWhile (<10000) . filter odd . map (^2) $
> > [1..]
>
> Why filter out the evens after generating them?
>
> > oos1 :: Integer
> > oss1 = sum . takeWhile (<10000) $ map (^2) odds
> >   where odds = iterate (+2) 1

Since we're now taking the code apart,

oddSquareSum2 :: Integer
oddSquareSum2 = sumOddSquaresBelow 10000

sumOddSquaresBelow bound = takeWhile (< bound) [x*x | x <- [1, 3 .. ]]

(but you'd really want to use the power-sum formula).
>
> Best wishes
>
> Stephen


More information about the Haskell-Cafe mailing list