December 2024
Beginner
456 pages
13h 37m
English
Haskell does not strictly evaluate its values but uses lazy evaluation. The short explanation of lazy evaluation is that values are only evaluated once they are needed; it’s call by need. However, this doesn’t tell the full story.
First of all, why is lazy evaluation useful ? Let’s look at the const function:
const :: a -> b -> a const x _ = x
As we can see, the second argument is always discarded. So what if the value of the second argument is very expensive to compute? The answer is it does not matter, since the value is not needed! This also extends to case distinction (e.g., if-then-else or pattern matching). Let’s say we have code like this:
expensive1 :: Int expensive1 = ... -- something terribly ...