Anonymous recursion: Difference between revisions

m
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>
 
==={{header|IS-BASICChipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="is-basicqbasic">100 PROGRAM "Fibonacc.bas"cls
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)
250230 END DEFend</syntaxhighlight>
{{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
190 LET FIB=0K
220 200 CASE ELSE
230 210 LET FIB=FIB(K-1)+FIB(K-2)
240 220 END SELECT
230 END DEF </syntaxhighlight>
 
=={{header|Bracmat}}==
Line 1,015 ⟶ 1,049:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">import extensions;
 
fib(n)
{
if (n < 0)
{ InvalidArgumentException.raise() };
^ (n) {
if (n {> 1)
{ if (n > 1)
^ this self(n {- 2) + (this self(n - 1))
}(n)
^ this self(n - 2) + (this self(n - 1))
}else
{ else
^ {n
^ n }
}(n)
}(n)
}
 
public program()
{
for (int i := -1,; i <= 10,; i += 1)
{
console.print("fib(",i,")=");
try
{
console.printLine(fib(i))
}
catch(Exception e)
{
console.printLine:("invalid")
}
};
console.readChar()
}</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.
 
'''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 casecases'''
 
[[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
200 CASE 1
210 LET FIB=1
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="ecmascriptwren">class Fibonacci {
static compute(n) {
var fib
Anonymous user