|
|
|
|
|
by mdkess
4601 days ago
|
|
I don't think that I really understood closures until I took Dan Grossman's Coursera course "Programming Languages." In it, we implemented in Racket a simple programming language which included closures, which is the point where it really clicked for me. In the class, he defines a closure as follows: "We have said that functions are values, but we have not been precise about what that value exactly is. We
now explain that a function value has two parts, the code for the function (obviously) and the environment
that was current when we created the function. These two parts really do form a “pair” but we put “pair” in
quotation marks because it is not an ML pair, just something with two parts. You cannot access the parts
of the “pair” separately; all you can do is call the function. This call uses both parts because it evaluates
the code part using the environment part. This “pair” is called a function closure or just closure. The reason is that while the code itself can have free
variables (variables that are not bound inside the code so they need to be bound by some outer environment),
the closure carries with it an environment that provides all these bindings. So the closure overall is “closed”
— it has everything it needs to produce a function result given a function argument." (section course notes: https://d396qusza40orc.cloudfront.net/proglang/lecture_slide...) |
|
[0] -- http://shop.oreilly.com/product/9780596802806.do