If we've defined the following:
lazyFib x y = x:(lazyFib y (x + y))
fib = lazyFib 1 1
(from the 7 languages in 7 weeks book). Why does
fibNth x = head (drop (x-1) fib)
evaluate slower than
fibNth2 x = head (drop (x-1) (take (x) fib)
? Clearly the second one terminates the infinite list as soon as required - but intuitively I expected the (head) call to terminate the evaluation the moment one item comes through the "drop", regardless of whether there was a take limit on fib? Can anyone explain?
(updated with timings for reference):
> :set +s
> fibNth 100000
259740693472217241661550340212759154148804853865176...
(1.16 secs, 458202824 bytes)
> fibNth2 100000
259740693472217241661550340212759154148804853865176....
(0.09 secs, 12516292 bytes)