This article provides a guide to lazy evaluation in Haskell, explaining its mechanics, benefits for modular code, and methods for analyzing space and time usage.
# The Incomplete Guide to Lazy Evaluation (in Haskell)
Source: [https://apfelmus.nfshost.com/articles/lazy-eval.html](https://apfelmus.nfshost.com/articles/lazy-eval.html)
*Originally, I wanted to write a complete guide to lazy evaluation, but then\.*
Lazy evaluation is the most widely used method for executing Haskell program code on a computer\. It determines the time and memory usage of Haskell programs, and it allows new and powerful ways to write modular code\. To make full use of purely functional programming, a good understanding of lazy evaluation is very helpful\.
The following series of tutorials provide a thorough introduction to this topic:
- [How does Lazy Evaluation Work in Haskell?](https://apfelmus.nfshost.com/articles/lazy-eval-intro.html)— Start here\. This tutorial explains how expressions are evaluated using lazy evaluation, and what that means for time and memory usage\.
- [Writing More Modular Code with Lazy Evaluation](https://apfelmus.nfshost.com/articles/lazy-eval-modular-code.html)— Why use lazy evaluation in the first place? This tutorial explains the power of lazy evaluation and how it can help us to write clear and modular code\. In particular, it will tell you about**infinite lists**\.
- [Non\-Strict Semantics of Haskell – What Exactly does Lazy Evaluation Calculate?](https://apfelmus.nfshost.com/articles/non-strict-semantics.html)– This tutorial explains that we actually do*not*need to know how lazy evaluation works in order to understand*what*a Haskell program calculates, thanks to a concept called**denotational semantics**\. We cover the important notion of a**strict**function, which shows up in many discussions about optimizing Haskell programs\. Lazy evaluation controls the*how*, but not the*what*of a Haskell program\.
Additionally, tools for reasoning about space and time usage are provided in the following articles:
- [Space invariants — Reasoning about space usage](http://apfelmus.nfshost.com/blog/2013/08/21-space-invariants.html)
- [Debit Method — Reasoning about running times](http://apfelmus.nfshost.com/articles/debit-method.html)
The article discusses methods to avoid correctness space leaks in Haskell by classifying leaks into strictness, liveness, and sharing types, and recommends defensive coding patterns and profiling techniques.
The article outlines a systematic method for improving Haskell build times by benchmarking, identifying bottlenecks like slow modules, analyzing causes such as excessive type-level programming, and evaluating trade-offs in optimizations.
This article explores techniques to emulate inline assembly in Haskell/GHC for invoking obscure CPU instructions and efficiently returning multiple values from foreign functions, using examples like widening multiplication and carry-less multiplication.
The article explores the optimal ApplicativeDo scheduling problem in GHC, which is disabled by default due to slowness, and draws an analogy to the dynamic programming algorithm used for RNA folding to improve compiler performance.
This article explains lazy initialization in C++ to optimize performance by deferring computations until necessary, using a helper struct to exploit conversion operators in std::optional.