I'm learning haskell and I have the following code:
fib a b = a : fib b (a + b) findFibSum = sum [x | x <- fib 1 2, mod x 2 == 0 && x < 100]
If I run
findFibSum nothing happens, it just sits there. Shouldn't
fib be evaluated and return each item? I'm guessing this has something to do with lazy evaluation.
If I call
take first and make
findFibSum accept the taken list like:
findFibSum $ take 100 $ fib 1 2
it works instead. How do I make it so that I can retrieve and check each item instead? I can probably get away by taking by batch using
take but I'd like to understand this first.
Thanks to @amalloy i finally got it working with:
findFibSum xs = sum [x | x <- takeWhile (<4000000) xs, mod x 2 == 0]
takeWhile ensures that it'll stop retrieving value from fib once it gets >= 4m.