Anonymous recursion: Difference between revisions
Content added Content deleted
(→{{header|Smalltalk}}: oops) |
|||
Line 2,785: | Line 2,785: | ||
Here the closure is defined as "_", and then evaluated (by sending it a <tt>value:</tt> message). |
Here the closure is defined as "_", and then evaluated (by sending it a <tt>value:</tt> message). |
||
<lang smalltalk> |
<lang smalltalk> |
||
myMethodComputingFib:arg |
|||
myMethodComputingFac:arg |
|||
|_| |
|_| |
||
^ (_ := [:n | n <= 1 |
^ (_ := [:n | n <= 1 |
||
ifTrue:[ |
ifTrue:[n] |
||
ifFalse:[n |
ifFalse:[(_ value:(n - 1))+(_ value:(n - 2))]] |
||
) value:arg.</lang> |
) value:arg.</lang> |
||
b) Define it in a local scope to not infect the outer scopes. |
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 |
<br>Here, a separate closure is defined (and evaluated with <tt>value</tt>), in which fib is defined and evaluated with the argument. |
||
This is semantically equivalent to the named let solution of Scheme. |
|||
<lang smalltalk> |
<lang smalltalk> |
||
myMethodComputingFib2:arg |
|||
myMethodComputingFac2:arg |
|||
^ [ |
^ [ |
||
| |
|fib| |
||
[:n | n <= 1 |
[:n | n <= 1 |
||
ifTrue:[1] |
ifTrue:[1] |
||
ifFalse:[n |
ifFalse:[(fib value:(n - 1))+(fun value:(n - 2))]] value:arg. |
||
] value.</lang> |
] value.</lang> |
||
To completely make it anonymous, we could use reflection to get at the current executed block (via thisContext), |
To completely make it anonymous, we could use reflection to get at the current executed block (via thisContext), |