Reputation: 435
Some guy asserts that the following piece of code is an illustration of closure in Lisp. I'm not familiar with Lisp, but believe he is wrong. I don't see any free variables, it seems to me as an example of ordinary high level functions. Could you please judge...
(defun func (callback)
callback()
)
(defun f1() 1)
(defun f1() 2)
func(f1)
func(f2)
Upvotes: 5
Views: 1307
Reputation: 9225
There is no function being defined inside func
that would enclose local variables inside func
. Here is a contrived example based on yours Here is a good example:
Input:
(define f
(lambda (first-word last-word)
(lambda (middle-word)
(string-append first-word middle-word last-word))))
(define f1 (f "The" "cat."))
(define f2 (f "My" "adventure."))
(f1 " black ")
(f1 " sneaky ")
(f2 " dangerous ")
(f2 " dreadful ")
Output:
Welcome to DrScheme, version 4.1.3 [3m].
Language: Pretty Big; memory limit: 128 megabytes.
"The black cat."
"The sneaky cat."
"My dangerous adventure."
"My dreadful adventure."
>
f
defines and returns a closure in which the first and last words are enclosed, and which are then reused by calling the newly-created functions f1
and f2
.
This post has several hundred views, therefore if non-schemers are reading this, here is the same silly example in python:
def f(first_word, last_word):
""" Function f() returns another function! """
def inner(middle_word):
""" Function inner() is the one that really gets called
later in our examples that produce output text. Function f()
"loads" variables into function inner(). Function inner()
is called a closure because it encloses over variables
defined outside of the scope in which inner() was defined. """
return ' '.join([first_word, middle_word, last_word])
return inner
f1 = f('The', 'cat.')
f2 = f('My', 'adventure.')
f1('black')
Output: 'The black cat.'
f1('sneaky')
Output: 'The sneaky cat.'
f2('dangerous')
Output: 'My dangerous adventure.'
f2('dreadful')
Output: 'My dreadful adventure.'
Upvotes: 18
Reputation: 7597
Here's my contribute as a JavaScript programmer:
A closure is a function that has access to variables defined in its lexical scope (a scope that might not be present anymore when the closure is actually invoked). Here:
function funktionFactory(context) {
// this is the lexical scope of the following anonymous function
return function() {
// do things with context
}
}
Once funktionFactory
has returned the lexical scope is gone forever BUT (and it's a big 'but') if the returned function is still referenced (and therefore not garbage collected), then such function (a closure) can still play with the original variable context
. Here:
var closure = funktionFactory({
name: "foo"
});
no one but closure
can access the name
property of the context object (unreachable for any other entity in the software when funktionFactory
has returned).
So to answer your question: is func
a closure? nope. And callback
? neither!
Upvotes: 0