Lesson 36 of 55
10 mins readPython Strictness, seq & Bang Patterns (!)
In Plain English
While laziness is powerful, unreduced thunks can cause memory space leaks in accumulators. Force evaluation using seq, strict application ($!), and bang patterns (!).
Deep Dive: How It Works
Weak Head Normal Form (WHNF): Evaluated just enough to see the outermost constructor.
seq: seq a b forces evaluation of a to WHNF before returning b.
Strict Application ($!): f $! x forces x to WHNF before calling f.
foldl': The strict version of foldl in Data.List that evaluates accumulators eagerly to prevent space leaks.
Core Rules to Remember

foldl' vs foldl: Always use foldl' for numerical summations to avoid space leaks.

seq and $!: Force immediate evaluation when laziness would create excess memory pressure.
Live Interactive Example
Hit Run Code to see it liveStrict Summation with foldl'
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: foldl' evaluated each sum step eagerly, using O(1) memory.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Run Strict Application ($!)
Compute `id $! (10 + 15)` and print `"Strict evaluated: "` followed by its `show` string.
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.