Lesson 24 of 55
10 mins readPython Lazy Evaluation & Infinite Streams
In Plain English
Haskell uses lazy evaluation (call-by-need). Expressions are only evaluated when their values are demanded by an I/O action or pattern match.
Deep Dive: How It Works
Thunks: Unevaluated expressions stored as memory objects awaiting evaluation.
Infinite Primitives: repeat x (infinite list of x), cycle xs (repeats xs infinitely), iterate f x (x, f x, f (f x), ...).
Infinite Fibonacci: fibs = 0 : 1 : zipWith (+) fibs (tail fibs).
Core Rules to Remember

Call-by-Need: Values are calculated at most once and shared across consumers.

Infinite Streams in Constant Space: Generate endless streams while consuming only what you need.
Live Interactive Example
Hit Run Code to see it liveInfinite Fibonacci Stream and cycle
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: fibs generates each number on-demand by summing previous stream elements.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Generate Powers of 2 with iterate
Take `5` elements from `iterate (* 2) 1`: `take 5 (iterate (* 2) 1)`.
Print `"Powers: "` followed by the result.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.