Jump to content

Scope modifiers: Difference between revisions

(→‎{{header|Common Lisp}}: Trim: too much information. This isn't Wikipedia. :))
Line 750:
2 → x
Return x^x</lang>
 
=={{header|TXR}}==
 
Functions and filters are global in TXR. Variables are pattern matching variables and have a dynamically scoped discipline. The binding established in a clause is visible to other clauses invoked from that clause, including functions. Whether or not bindings survive from a given scope usually depends on whether the scope, overall, failed or succeeded. Bindings established in scopes that terminate by failing (or by an exception) are rolled back and undone. The <code>@(local)</code> or <code>@(forget)</code> directives, which are synonyms, are used for breaking the relationship between variables occuring in a scope, and any bindings those variables may have. If a clause declares a variable forgotten, but then fails, then this forgetting is also undone; the variable is known once again. But in successful situations, the effects of forgetting can be passed down.
 
Functions have special scoping and calling rules. No binding for a variable established in a function survives the execution of the function, except if its symbol matches one of the function parameters, call it P, and that parameter is unbound (i.e. the caller specified some unbound variable A as the argument). In that case, the new binding for unbound parameter P within the function is translated into a new binding for unbound argument A at the call site. Of course, this only happens if the function succeeds, otherwise the function call is a failure with no effect on the bindings.
 
Illustration using named blocks. In the first example, the block succeeds and so its binding passes on:
 
<lang txr>@(maybe)@# perhaps this subclause suceeds or not
@ (block foo)
@ (bind a "a")
@ (accept foo)
@(end)
@(bind b "b")</lang>
 
Result:
 
<pre>a="a"
b="b"</pre>
 
By contrast, in this version, the block fails. Because it is contained in a <code>@(maybe)</code>, evaluation can proceed, but the binding for <code>a</code> is gone.
 
<lang txr>@(maybe)@# perhaps this subclause suceeds or not
@ (block foo)
@ (bind a "a")
@ (fail foo)
@(end)
@(bind b "b")</lang>
 
Result:
 
<pre>b="b"</pre>
 
=={{header|Ursala}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.