Why doesn't this function bottom out
5
xs = [1, 2] ++ undefined
length $ take 2 $ take 4 xs
My brain is reading this as
length (take 2 ( take 4 xs ) )
If everything in the parenthesis is evaluated first, then why does this not error out with Exception: prelude.undefined?
The answer given by the book is that take 2 is only taking the first two indices, but shouldn't the take 4 take precedence here and get evaluated first?
haskell
-
1It's not true in Haskell that "Everything in parentheses is evaluated first". Rather the outermost expression is generally evaluated first (inner expressions being evaluated when and only if they are needed to "unblock" the evaluation of the outermost expression). That's pretty much what lazy evaluation is. – Ben 2 days ago
Add a comment
|