Jump to content

Anonymous recursion: Difference between revisions

Line 2,785:
Here the closure is defined as "_", and then evaluated (by sending it a <tt>value:</tt> message).
<lang smalltalk>
myMethodComputingFib:arg
myMethodComputingFac:arg
|_|
 
^ (_ := [:n | n <= 1
ifTrue:[1n]
ifFalse:[(_ value:(n *- 1))+(_ value:(n - 12))]]
) value:arg.</lang>
b) Define it in a local scope to not infect the outer scopes.
<br>Here, a separate closure is defined (and evaluated with <tt>value</tt>), in which the localFac closurefib is defined and evaluated with the argument.
This is semantically equivalent to the named let solution of Scheme.
<lang smalltalk>
myMethodComputingFib2:arg
myMethodComputingFac2:arg
^ [
|localFacfib|
 
[:n | n <= 1
ifTrue:[1]
ifFalse:[(fib value:(n *- 1))+(localFacfun value:(n - 12))]] value:arg.
] value.</lang>
To completely make it anonymous, we could use reflection to get at the current executed block (via thisContext),
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.