Project Euler 2: Haskell implementation confusion -


I am using GHCI to try to solve problem 2 on Project Over.

I have defined infinite list fibers:

prelude & gt; Fibs = 1: 2: Zip (+) Fibers (tail fib)

I tried to use the list of the following methods:

Introduction & gt; [X | X & lt; -fibs, x mod 2 == 0, x & lt; 4000000] [1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181, 6765,10946,17711,28657,46368,75025,121393,196418,317811,514229, 832040,1346269,2178309,3524578
Prelude & gt; Amount $ [x | X & LT; - [1 ..], x mod 2 == 0, x & lt; 4000000]

But the shell hangs on another order. I am confused as to why the compilation of the list can create a list, but the zodiac function can not process it.

I found that the work solution is

prelude & gt; The amount of $ Filters also takes $ (fibers of & lt; = 4000000)

but once again I am confused that why does the method not understand when the list works.

while evaluating

  [x | X & lt; -fibs, x mod 2 == 0, x & lt; 4000000]   

Your program / hoskle compiler does not know that this list is really finite when you call a function that completely consumes a list like sum , Then it is only generating Fibonacci numbers, to give them the x & lt; Testing for 4000000 (and failed every time after a certain point).

In fact, the Haskell compiler can not know in the general case whether an expression expression represents a limited list; there is a problem.

Comments