Closures/Value capture: Difference between revisions

imported>Rowsety Moid
No edit summary
imported>Rowsety Moid
Line 29:
=={{header|Acornsoft Lisp}}==
 
Since this Lisp is dynamically scoped and does not have any built-in closure mechanism, we have to construct one which we'll call <code>freeze</code>. (The name is inspired by the [[Pop-2]] programming languages's "frozen formals".)
 
('''freeze''' ''varlist lambda-expr'') finds the current values of the variables in ''varlist'' and returns a lambda-expression that is like the original except that, when called, it binds those variables to their captured values. For example, if <code>a</code>'s value is 1 and <code>b</code>'s is 2,
<syntaxhighlight lang="lisp">
( (lambda ((a . 1) (b . 2))
(list a b)) )
</syntaxhighlight>
 
<syntaxhighlight lang="lisp">
Line 47 ⟶ 44:
(list a b c))))
</syntaxhighlight>
 
What does that mean? A cons (''name'' . ''value'') in a lambda-expressions's formal parameters is the syntax for a formal with a default value. The ''value'' is literally the value; it's not an expression that's evaluated. This
 
<syntaxhighlight lang="lisp">
( (lambda ((a . 1) (b . 2))
(list a b c)) )
</syntaxhighlight>
 
calls the function represented by that lambda-expression. Since it does not give the function any arguments, <code>a</code> and <code>b</code> get their default values (which are the values captured by <code>freeze</code>).
 
Here is the definition of <code>freeze</code>:
Anonymous user