Find limit of recursion: Difference between revisions

Content added Content deleted
(add tinybasic)
(→‎{{header|AppleScript}}: Added a third test.)
Line 233: Line 233:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
===Test 1===
A basic test for Applescript, which has a notoriously shallow recursion stack.
A basic test for Applescript, which has a notoriously shallow recursion stack.
<lang applescript>-- recursionDepth :: () -> IO String
<lang applescript>-- recursionDepth :: () -> IO String
Line 257: Line 258:
<pre>"Recursion limit encountered at 502"</pre>
<pre>"Recursion limit encountered at 502"</pre>


===Test 2===
We get a fractionally higher (and arguably purer) result by deriving the highest Church Numeral (Church-encoded integer) that can be represented using AppleScript:
We get a fractionally higher (and arguably purer) result by deriving the highest Church Numeral (Church-encoded integer) that can be represented using AppleScript:
<lang applescript>-- HIGHEST CHURCH NUMERAL REPRESENTABLE IN APPLESCRIPT ?
<lang applescript>-- HIGHEST CHURCH NUMERAL REPRESENTABLE IN APPLESCRIPT ?
Line 354: Line 356:
{{Out}}
{{Out}}
<pre>"The highest Church-encoded integer representable in Applescript is 571"</pre>
<pre>"The highest Church-encoded integer representable in Applescript is 571"</pre>
----
===Test 3===
The recursion limit with a fixed-length stack depends not only on the size of the stack, but on how many local variables (including parameter variables) and return addresses (including those for 'try' statements) are stored at each level. Also, of course, on where you start counting, since recursion's unlikely to begin with an empty stack in real-life situations and you may or may not regard the top call to a recursive handler as part of the recursion.

The recursive handler in the first AppleScript test above is entered with the return addresses from the 'run' and 'recursionDepth' handlers (or pointers thereto) already on the stack along with pointers to the local 'go' value and the passed 'i'. Each successive call stacks the return addresses for the 'try' statement and the handler itself along with a new 'i'. The final result of 502 is the number of times the recursive handler successfully calls itself and is probably a reasonably indicative real-world figure.

Testing with no local variables and with an external 'try' statement, the maximum recursion depth possible appears to be 733. (732 if the code below's run as an applet instead of in an editor or from the system script menu.) So, depending on what a script actually does, the limit can be anything between <502 and 733. In practice, it's very difficult for well written AppleScript code to run out of stack.

<lang applescript>global i

on |λ|()
set i to i + 1
|λ|()
end |λ|

on run
set i to -1
try
|λ|()
on error
"Recursion limit encountered at " & i
-- display dialog result -- Uncomment to see the result if running as an applet.
end try
end run</lang>

{{output}}
<lang applescript>"Recursion limit encountered at 733"</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==