4 ms·
How do they figure out what goes into their environment, or how do their environments get populated? I believe the first happens at compile time, and the secon
by mrkeen 3y ago
How do they figure out what goes into their environment, or how do their environments get populated?
I believe the first happens at compile time, and the second happens after the function is passed.
1 f x =
2 let xx = x * x in
3 (\y -> y + xx)
4
5 main = do
6 let clo = f 2
7 let applied = clo 3
8 print applied
The closure is defined on line 3, and instantiated on line 6.
xx is known to be the captured variable at compile time. But in a lazy setting, it's fine for the closure to be passed around before the value of xx is known:
f x =
let xx = trace "Evaluating xx" (x * x) in
trace "Returning closure" (\y -> y + xx)
main = do
let clo = f 2
let applied = clo 3
print applied
> Returning closure
> Evaluating xx
> 7