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

Ketil Malde ketil at malde.org
Wed Mar 10 09:21:07 EST 2010


Stephen Tetley <stephen.tetley at gmail.com> writes:

>> oddSquareSum :: Integer
>> oddSquareSum = sum . takeWhile (<10000) . filter odd . map (^2) $ [1..]

> Why filter out the evens after generating them?

In other words:

  sum . takeWhile (<10000) . filter odd . map (^2) $ [1..]

Since odd (x^2) => odd x:

  sum . takeWhile (<10000) . map (^2) $ [1,3..]

Although it doesn't matter (more than a constant at any rate) for
complexity, why generate values only to trim them later?

Since x^2 < 10000 => x < 100:

  sum $ map (^2) [1,3..99]

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list