Skip to content

Closures/Value capture

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Collections.LIST
let squares = LIST[() -> int]()
for i in 0..10 do
squares.add(() => i * i)
od
write_line("{squares[3]()}")
9