4 ms·
foo and bar are both lambdas and closures. A named lambda is still a lambda. foo = function (x) { return x + 1; } is identical to function foo (x) { retu
by sillysaurus3 9y ago
foo and bar are both lambdas and closures. A named lambda is still a lambda.
foo = function (x) { return x + 1; }
is identical to
function foo (x) { return x + 1; }
They are also closures. Since they don't refer to any outer variables, they are closures containing zero context variables. If foo were to refer to some other variable, the closure would contain one context variable.
The only reason to distinguish between lambdas and closures is because some languages do not support closures (e.g. early versions of Emacs Lisp). Attempting to refer to other variables would always refer to a global variable with that name.
- saghm 9y ago> foo and bar are both lambdas and closures. A named lambda is still a lambda. > foo = function (x) { return x + 1; } > is identical to > function foo (x) { return x + 1; } I'll concede that for JavaScript (and a few other languages); some languages don't treat all functions as just a lambda with a binding, though. > They are also closures. Since they don't refer to any outer variables, they are closures containing zero context variables. If foo were to refer to some other variable, the closure would contain one context variable. I'm not very keen on the definition of "closure" including functions that close over zero variables, because then "closure" becomes indistinguishable from "function", and the word is no longer useful. I might be wrong about the technical definition, but I feel like having a word for closures isn't very useful if it doesn't mean "closing over one or more variables".
- sillysaurus3 9y agoElisp disagrees: > (lambda () (print "hi")) (closure (t) nil (print "hi")) > (let ((a 1) (b 2)) (lambda () (print "hi"))) (closure ((b . 2) (a . 1) t) nil (print "hi")) > (let ((a 1)) (defun foo (x) (+ x 1))) foo > (symbol-function 'foo) (closure ((a . 1) t) (x) (+ x 1)) > (defun bar (x) (+ x 1)) bar > (symbol-function 'bar) (closure (t) (x) (+ x 1)) So yes, every function is a closure.
- kukx 9y agoIt depends on your definition of the closure. Example: "A closure is the combination of a function and the lexical environment within which that function was declared." [0] [0] https://developer.mozilla.org/en/docs/Web/JavaScript/Closures https://developer.mozilla.org/en/docs/Web/JavaScript/Closure...
- joshuamorton 9y agoA lexical environment being syonymous with "a set of 0 or more variables accessible from within the function, but not passed as arguments." The key piece of that being 0 or more. The null set is still a set.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- kukx 9y agoI do not think it is useful to interpret it as 0 or more. To me it only make sense to assume 1 or more. Otherwise you could say that the programming language that do not support closing variables, has nonetheless closures, as according to that notion every function is a closure that closes nothing.
- TeMPOraL 9y agoI think this is a rectangle/square problem. When you say you accept/return a closure, you probably mean a "function + lexical scope of 0 or more variables". You can still refer to "non-closures" as functions, though. It's like code expecting rectangles wouldn't mind to get squares, but a code expecting squares will definitely do mind getting rectangles.
- kukx 9y ago
- kukx 9y ago>A named lambda is still a lambda. Most commonly a lambda is regarded as an anonymous function. [0] If you name it, it is no longer anonymous. [0] - https://softwareengineering.stackexchange.com/questions/130722/what-is-the-difference-between-a-function-and-a-lambda/130785 https://softwareengineering.stackexchange.com/questions/1307...
- barrkel 9y agoA named lambda isn't.