Skip to Content
Learn Haskell by Example
book

Learn Haskell by Example

by Philipp Hagenlocher
December 2024
Beginner content levelBeginner
456 pages
13h 37m
English
Manning Publications
Content preview from Learn Haskell by Example

appendix B Lazy evaluation

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Get Programming with Haskell

Get Programming with Haskell

Will Kurt
Haskell in Depth

Haskell in Depth

Vitaly Bragilevsky

Publisher Resources

ISBN: 9781633438934Supplemental ContentPublisher SupportOtherPublisher WebsiteSupplemental ContentErrata PagePurchase Link