Jump to content

Find limit of recursion: Difference between revisions

no edit summary
(Added Befunge example.)
No edit summary
Line 1,330:
333325 false D:\00EXE\share\lua\5.3\test.lua:57: stack overflow
</pre>
 
=={{header|M2000 Interpreter}}==
===Modules & Functions===
<lang M2000 Interpreter>
Module checkit {
Global z
Function a {
z++
=a()
}
try {
m=a()
}
Print z
z<=0
Function a {
z++
call a()
}
try {
call a()
}
Print z
z<=0
Module m {
z++
Call m
}
try {
call m
}
Print z
z<=0
\\ without Call a module can't call itself
\\ but can call something global, and that global can call back
Module Global m1 {
z++
m2
}
Module Global m2 {
z++
m1
}
try {
call m1
}
Print z
}
Checkit
</lang>
In Wine give these:
<pre>
4030
8473 (plus 2 because we have Chgeckit inside a Z so these are two calls)
8473 (the same as above)
11225 (the same as abive)
</pre>
 
===Subroutines===
A lot of languages have a subroutine as a function without return value. As we see before, M2000 has Modules (as procedures) and Functions as that can be called as procedures too. These "procedures" can use only globals and anything they make for them.
So what is a subroutine ιν Μ2000?
 
Subroutines are part of modules/functions. They haven't execution object, and they have to use parent object. So this parent object has the return stack, and use heap for this. So we can set a limit with Recursion.Limit to say 500000.
 
So a subroutine is code with module's scope, with recursion and local definitions. Utilizing current stack we can get results, or we can use by reference parameters to get results too.
 
We have to use statement Local for local variables and arrays who shadows same name variables or arrays. Parent can be the module/function as the caller, or another subroutine, or the same, but all have the same parent, the module/function.
 
 
<lang M2000 Interpreter>
Module Checkit {
\\ recursion for subs controled by a value
\\ change limit get a list of numbers from 1 to limit
Recursion.Limit 10
function FindZ {
z=1
Try {
CallmeAgain(1)
}
=Abs(z)
Sub CallmeAgain(x)
z--
CallmeAgain(x+1)
z++
End Sub
}
z=FindZ()
Print "Calls:"; z
NormalCall(1)
Sub NormalCall(x)
Print x
z--
if z>0 then NormalCall(x+1)
z++
End Sub
}
Checkit
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.