Tail recursion, and limits

Alex Tweedly alex at tweedly.net
Fri Apr 26 13:31:53 EDT 2013


It should only call itself 900 times (or maybe 899 :-)

But the recursionLimit is not (as you might expect) the max depth of 
recursion allowed, as implied by
      Summary:
      Specifies how many levels deep a recursive function is allowed to go.

The spec in the docs says
     set the recursionLimit to stackSizeInBytes
and
   The stackSizeInBytes specifies the CPU call stack size.

So the recursionLimit is set 400,000 - and that may be reached by far 
fewer than 900 recursive calls.
In this case - with two params and a return value - it dies at around 520

on mouseUp
    answer the recursionlimit -- this answers 400,000
    repeat with i = 90 to 900 step 10
       try
          put incrementer(1,i)
       catch tError
          exit repeat
       end try
    end repeat
    put "   fail with" && i &CR after msg

end mouseUp

function incrementer x,steps
    if steps < 1 then return x else return incrementer(x+1,steps-1)
end incrementer

-- Alex.




On 26/04/2013 17:22, Geoff Canyon wrote:
> So I was curious whether LC optimizes tail calls. It appears not. But even
> stranger, this code busts the recursion limits. It should only call itself
> 900 times, right? And am I correct that if LC optimized tail calls, then
> this would work regardless of the recursionLimit.
>
>
> on mouseUp
>     answer the recursionlimit -- this answers 400,000
>     put incrementer(1,900)
> end mouseUp
>
> function incrementer x,steps
>     if steps < 1 then return x else return incrementer(x+1,steps-1)
> end incrementer
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list