Find limit of recursion: Difference between revisions

Content added Content deleted
(rearranges in order of the language.)
Line 63:
 
Last visible number is 827.
 
=={{header|AutoIt}}==
<lang AutoIt>;AutoIt Version: 3.2.10.0
Line 73 ⟶ 74:
Last value of $depth is 5099 before error.
Error: Recursion level has been exceeded - AutoIt will quit to prevent stack overflow.
 
=={{header|AWK}}==
<lang AWK># syntax: GAWK -f FIND_LIMIT_OF_RECURSION.AWK
Line 96 ⟶ 98:
x()
}</lang>
 
=={{header|BASIC}}==
==={{header|ZX Spectrum Basic}}===
On the ZX Spectrum recursion is limited only by stack space. The program eventually fails, because the stack is so full that there is no stack space left to make the addition at line 110:
<lang zxbasic>
10 LET d=0: REM depth
100 PRINT AT 1,1; "Recursion depth: ";d
110 LET d=d+1
120 GO SUB 100: REM recursion
130 RETURN: REM this is never reached
200 STOP
</lang>
 
{{out}} (from a 48k Spectrum):
{{{
Recursion depth: 13792
4 Out of memory, 110:1
}}}
 
=={{header|Batch File}}==
Line 161 ⟶ 181:
set /a c=%1-1
echo [Level %c%] No good</lang>
 
=={{header|BASIC}}==
==={{header|ZX Spectrum Basic}}===
On the ZX Spectrum recursion is limited only by stack space. The program eventually fails, because the stack is so full that there is no stack space left to make the addition at line 110:
<lang zxbasic>
10 LET d=0: REM depth
100 PRINT AT 1,1; "Recursion depth: ";d
110 LET d=d+1
120 GO SUB 100: REM recursion
130 RETURN: REM this is never reached
200 STOP
</lang>
 
{{out}} (from a 48k Spectrum):
{{{
Recursion depth: 13792
4 Out of memory, 110:1
}}}
 
=={{header|BBC BASIC}}==
Line 251 ⟶ 253:
}</lang>
With GCC 4.5, if compiled without -O2, it segfaults quickly; if <code>gcc -O2</code>, crash never happens, because the optimizer noticed the tail recursion in recur() and turned it into a loop!
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
class RecursionLimit
{
static void Main(string[] args)
{
Recur(0);
}
private static void Recur(int i)
{
Console.WriteLine(i);
Recur(i + 1);
}
}</lang>
 
Through debugging, the highest I achieve is 14250.
 
Through execution (with Mono), another user has reached 697186.
 
=={{header|Clojure}}==
<lang clojure>
=> (def *stack* 0)
=> ((fn overflow [] ((def *stack* (inc *stack*))(overflow))))
java.lang.StackOverflowError (NO_SOURCE_FILE:0)
=> *stack*
10498
</lang>
 
=={{header|COBOL}}==
Line 408 ⟶ 439:
However, for an implementation of Lisp that supports proper tail recursion,
this function will not cause a stack overflow, so this method will not work.
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
class RecursionLimit
{
static void Main(string[] args)
{
Recur(0);
}
private static void Recur(int i)
{
Console.WriteLine(i);
Recur(i + 1);
}
}</lang>
 
Through debugging, the highest I achieve is 14250.
 
Through execution (with Mono), another user has reached 697186.
 
=={{header|Clojure}}==
<lang clojure>
=> (def *stack* 0)
=> ((fn overflow [] ((def *stack* (inc *stack*))(overflow))))
java.lang.StackOverflowError (NO_SOURCE_FILE:0)
=> *stack*
10498
</lang>
 
=={{header|D}}==
Line 454 ⟶ 456:
 
Using -O compilation argument DMD performs tail call optimization, and the stack doesn't overflow.
 
=={{header|Déjà Vu}}==
{{untested|Déjà Vu}}
<lang dejavu>rec-fun n:
!. n
rec-fun ++ n
 
rec-fun 0</lang>
This continues until the memory is full, so I didn't wait for it to finish.
Currently, it should to to almost 3 million levels of recursion on a machine with 1 GB free.
Eliminating the <code>n</code> should give over 10 million levels on the same machine.
 
=={{header|Delphi}}==
Line 481 ⟶ 494:
{{out}}
<pre>Recursion Level is 28781</pre>
 
=={{header|Déjà Vu}}==
{{untested|Déjà Vu}}
<lang dejavu>rec-fun n:
!. n
rec-fun ++ n
 
rec-fun 0</lang>
This continues until the memory is full, so I didn't wait for it to finish.
Currently, it should to to almost 3 million levels of recursion on a machine with 1 GB free.
Eliminating the <code>n</code> should give over 10 million levels on the same machine.
 
=={{header|DWScript}}==
Line 515 ⟶ 517:
 
Outside of debugging access to other vats, E programs are (ideally) not allowed to observe recursion limits, because stack unwinding at an arbitrary point can break invariants of the code that was executing at the time. In particular, consider an attacker who estimates the stack size, nearly fills up the stack to that point, then invokes the victim — If the attacker is allowed to catch our hypothetical StackOverflowException from inside the victim, then there is a good chance of the victim then being in an inconsistent state, which the attacker can then make use of.
 
=={{header|Elixir}}==
same as "Erlang"
 
=={{header|Emacs Lisp}}==
Line 530 ⟶ 535:
=={{header|Erlang}}==
Erlang has no recursion limit. It is tail call optimised. If the recursive call is not a tail call it is limited by available RAM. Please add what to save on the stack and how much RAM to give to Erlang and I will test that limit.
 
=={{header|F_Sharp|F#}}==
A tail-recursive function will run indefinitely without problems (the integer will overflow, though).
 
<lang fsharp>let rec recurse n =
recurse (n+1)
 
recurse 0</lang>
The non-tail recursive function of the following example crashed with a <code>StackOverflowException</code> after 39958 recursive calls:
 
<lang fsharp>let rec recurse n =
printfn "%d" n
1 + recurse (n+1)
 
recurse 0 |> ignore</lang>
 
=={{header|Forth}}==
Line 583 ⟶ 604:
Segmentation fault (core dumped)</pre>
 
 
=={{header|F_Sharp|F#}}==
A tail-recursive function will run indefinitely without problems (the integer will overflow, though).
 
<lang fsharp>let rec recurse n =
recurse (n+1)
 
recurse 0</lang>
The non-tail recursive function of the following example crashed with a <code>StackOverflowException</code> after 39958 recursive calls:
 
<lang fsharp>let rec recurse n =
printfn "%d" n
1 + recurse (n+1)
 
recurse 0 |> ignore</lang>
=={{header|GAP}}==
The limit is around 5000 :
Line 1,056 ⟶ 1,061:
print error ; 16 Stopping... recurse [make "depth :depth + 1]
(print [Depth reached:] :depth) ; some arbitrarily large number</lang>
 
=={{header|Lua}}==
<lang Lua>
counter = 0
 
function test()
print("Depth:", counter)
counter = counter + 1
test()
end
 
test()
</lang>
 
=={{header|LSL}}==
Line 1,101 ⟶ 1,093:
[2012/07/07 18:41] Object: Stack-Heap Collision
</pre>
 
=={{header|Lua}}==
<lang Lua>
counter = 0
 
function test()
print("Depth:", counter)
counter = counter + 1
test()
end
 
test()
</lang>
 
=={{header|Mathematica}}==
Line 1,139 ⟶ 1,144:
ИП0 ИП2 - x<0 20 ИП0 1 + П0 ПП 05
ИП1 1 + П1 В/О</lang>
 
=={{header|MUMPS}}==
<lang MUMPS>RECURSE
IF $DATA(DEPTH)=1 SET DEPTH=1+DEPTH
IF $DATA(DEPTH)=0 SET DEPTH=1
WRITE !,DEPTH_" levels down"
DO RECURSE
QUIT</lang>
End of the run ...<pre>
1918 levels down
1919 levels down
1920 levels down
DO RECURSE
^
<FRAMESTACK>RECURSE+4^ROSETTA
USER 72d0>
</pre>
 
=={{header|Modula-2}}==
Line 1,183 ⟶ 1,171:
0 1 523264 testfile</lang>
So the recursion depth is just over half a million.
 
=={{header|MUMPS}}==
<lang MUMPS>RECURSE
IF $DATA(DEPTH)=1 SET DEPTH=1+DEPTH
IF $DATA(DEPTH)=0 SET DEPTH=1
WRITE !,DEPTH_" levels down"
DO RECURSE
QUIT</lang>
End of the run ...<pre>
1918 levels down
1919 levels down
1920 levels down
DO RECURSE
^
<FRAMESTACK>RECURSE+4^ROSETTA
USER 72d0>
</pre>
 
=={{header|NetRexx}}==
Line 1,281 ⟶ 1,286:
(* Since with have eaten some stack with this function, the result is slightly lower.
But now it may be used inside any function to get the available stack space *)</lang>
 
 
=={{header|Oforth}}==
Line 1,378 ⟶ 1,382:
</pre>
This is because the Parrot VM currently imposes a limit of 1000. On the other hand, the niecza implementation has no limit, subject to availability of virtual memory. In any case, future Perl&nbsp;6 is likely to require tail call elimination in the absence of some declaration to the contrary.
 
=={{header|PHP}}==
<lang PHP><?php
function a() {
static $i = 0;
print ++$i . "\n";
a();
}
a();</lang>
 
{{out}}
<pre>
1
2
3
[...]
597354
597355
597356
597357
597358
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in [script-location.php] on line 5
</pre>
 
=={{header|PicoLisp}}==
Line 1,412 ⟶ 1,440:
Stack overflow
?</pre>
 
=={{header|PHP}}==
<lang PHP><?php
function a() {
static $i = 0;
print ++$i . "\n";
a();
}
a();</lang>
 
{{out}}
<pre>
1
2
3
[...]
597354
597355
597356
597357
597358
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in [script-location.php] on line 5
</pre>
 
=={{header|PL/I}}==