Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between closure in Scheme and usual closure in other languages?

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 enclosed environment variables.

Am I wrong?

like image 628
yuanqili Avatar asked Apr 27 '15 23:04

yuanqili


People also ask

What languages have closures?

Some languages have features which simulate the behavior of closures. In languages such as Java, C++, Objective-C, C#, VB.NET, and D, these features are the result of the language's object-oriented paradigm.

Do all programming languages have closures?

All functional programming languages that I know of (e.g. Haskell, Scala, Scheme, Clojure, SML, OCaml, ...) support a notion of closures. Also, I often read that a language X can be considered functional because it supports closures.

What is a closure scheme?

Scheme procedure's aren't really just pieces of code you can execute; they're closures. A closure is a procedure that records what environment it was created in. When you call it, that environment is restored before the actual code is executed.

Why are closures useful?

Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object's properties) with one or more methods.


1 Answers

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.

like image 169
John Clements Avatar answered Oct 05 '22 12:10

John Clements