Is there any difference between closure in Scheme

2019-05-04 17:29发布

问题:

I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages.

Here's what SICP says:

The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation.

Here closure is more close to closure in Mathematics I think, not what I have seen in JavaScript, which means the ability of a function to access enclosure environment variables.

Am I wrong?

回答1:

You're right; this text is not referring to "closures"--an implementation strategy to ensure that functions-as-values refer correctly to lexical bindings--but more generally to the mathematical notion of "closure", as for instance in the statement "the integers are closed under the addition operation". That is: applying the operation to any two elements of the set produces a result that is still a member of the set.



回答2:

There is a difference in the use of "closure" in SICP from the way it is typically used in computing. From SICP Chapter 2, footnote 6:

The use of the word 'closure' here comes from abstract algebra, where a set of elements is said to be closed under an operation if applying the operation to elements in the set produces an element that is again an element of the set. The Lisp community also (unfortunately) uses the word 'closure' to describe a totally unrelated concept: A closure is an implementation technique for representing procedures with free variables. We do not use the word 'closure' in this second sense in this book.

On the other hand, Schemer's use "closure" to refer to lexical closures just like programmers using other languages with lexical closures.