Anonymous user
Anonymous recursion: Difference between revisions
m
→{{header|Elena}}
imported>Arakov |
|||
(11 intermediate revisions by 7 users not shown) | |||
Line 18:
;Task:
If possible, demonstrate this by writing the recursive version of the fibonacci function (see [[Fibonacci sequence]]) which checks for a negative argument before doing the actual recursion.
;Related tasks:
:* [[Y combinator]]
<br><br>
Line 448 ⟶ 451:
Invalid argument: -10
55</pre>
{{works with|Chipmunk Basic|3.6.4}}
110 sub fib(num)
120 if num < 0 then print "Invalid argument: "; : fib = num
130 if num < 2 then fib = num else fib = fib(num-1)+fib(num-2)
140 end sub
190 print fib(20)
200 print fib(30)
210 print fib(-10)
220 print fib(10)
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|BBC BASIC}}===
Line 462 ⟶ 480:
55
</pre>
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Fibonacc.bas"
110 FOR I=0 TO 10▼
120 PRINT FIB(I);▼
130 NEXT ▼
140 DEF FIB(K)▼
150 SELECT CASE K▼
160 CASE IS<0▼
170 PRINT "Negative parameter to Fibonacci.":STOP ▼
180 CASE 0,1▼
230 END DEF </syntaxhighlight>
=={{header|Bracmat}}==
Line 1,015 ⟶ 1,049:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
fib(n)
{
if (n
{
^ this self(n
{
^
▲ }(n)
}
public program()
{
}</syntaxhighlight>
{{out}}
Line 1,354 ⟶ 1,387:
{{FormulaeEntry|page=https://formulae.org/?script=examples/Anonymous_recursion}}
'''Solution.'''
'''Solution.''' It consists in having a local function inside the main function, so it is neither visible nor available outside. The local function is defined after the validation, so if the input is invalid, neither the definition nor its invocation is performed.▼
▲
[[File:Fōrmulæ - Anonymous recursion 01.png]]
'''Test
[[File:Fōrmulæ - Anonymous recursion 02.png]]
Line 1,616 ⟶ 1,651:
fib2(x floor)
)</syntaxhighlight>
▲=={{header|IS-BASIC}}==
▲<syntaxhighlight lang="is-basic">100 PROGRAM "Fibonacc.bas"
▲110 FOR I=0 TO 10
▲120 PRINT FIB(I);
▲130 NEXT
▲140 DEF FIB(K)
▲150 SELECT CASE K
▲160 CASE IS<0
▲170 PRINT "Negative parameter to Fibonacci.":STOP
▲180 CASE 0
▲190 LET FIB=0
▲220 CASE ELSE
▲230 LET FIB=FIB(K-1)+FIB(K-2)
▲240 END SELECT
▲250 END DEF</syntaxhighlight>
=={{header|J}}==
Line 2,949 ⟶ 2,966:
144
</pre>
=={{header|RPL}}==
===Hidden variable===
The recursive part of the function is stored in a local variable, which is made accessible to all the recursive instances by starting its name with the <code>←</code> character.
{{works with|HP|48G}}
≪ ≪ '''IF''' DUP 1 > '''THEN'''
DUP 1 - ←fib EVAL
SWAP 2 - ←fib EVAL +
'''END''' ≫ → ←fib
≪ '''IF''' DUP 0 <
'''THEN''' DROP "Negative value" DOERR
'''ELSE''' ←fib EVAL '''END'''
≫ ≫ '<span style="color:blue">FIBAR</span>' STO
-2 <span style="color:blue">FIBAR</span>
10 <span style="color:blue">FIBAR</span>
{{out}}
<pre>
1: 55
</pre>
===Truly anonymous===
Both the recursive block and the argument are pushed onto the stack, without any naming. This meets the requirements of the task perfectly and works on any RPL machine, but it is far from idiomatic and uses a lot of stack space.
{{works with|HP|28}}
≪ '''IF''' DUP 0 <
'''THEN''' DROP "Negative value"
'''ELSE'''
≪ '''IF''' DUP 1 > '''THEN'''
DUP2 1 - OVER EVAL
ROT ROT 2 - OVER EVAL +
'''ELSE''' SWAP DROP '''END'''
≫
SWAP OVER EVAL
'''END'''
≫ '<span style="color:blue">FIBAR</span>' STO
=={{header|Ruby}}==
Line 3,566 ⟶ 3,617:
=={{header|Wren}}==
<syntaxhighlight lang="
static compute(n) {
var fib
|