3 ms·
I think you don't understand the Haskell runtime. let x = [1..] in do print (x !! 1000000) print (x !! 1000000) Element #1000000 (counting f
by klodolph 16y ago
I think you don't understand the Haskell runtime.
let x = [1..]
in do print (x !! 1000000)
print (x !! 1000000)
Element #1000000 (counting from zero) of the list "x" is printed out twice. The first time, "x" is a thunk. That thunk is evaluated, and the runtime stores the resulting value back into "x". The second time, "x" is a constructor, so it does not need to be evaluated. You'll see the memory usage spike after the first "print" to account for the million cons cells allocated.
This is no different than the interface provided by the memoization function. What distinguishes pure from impure is not memory access -- memory MUST always be modified in order to perform calculation. I recommend reading the Haskell report section on semantics.