Recursive calls of procedures lead to "undefined" runtime errors

648 views
Skip to first unread message

Sven Wiebus

unread,
Oct 17, 2014, 7:07:17 PM10/17/14
to mitappinv...@googlegroups.com

Hello MIT,

From what I found on Google recursion should be possible in App Inventor 2. However, when I try even simple examples like recursively calculating the factorial of a number, I always end up with an "undefined" runtime error. Attached is a tiny example (screen-shot and project file). For sake of completeness:

Factorial(x) :=
1                           for x <= 0 (forgive my laziness not handling x<0 as mathematically undefined in this example :)
x * Factorial(x-1)     for x>0

Is this a known issue or am I doing something wrong?

Regards
Sven

P. S. Of course I know that I could easily calculate the factorial of a number by iteration avoiding recursion (as every recursion can be translated into an iteration). It's just an example! :)

*** using AI2 in Firefox 32.0.3 on Win7 with a Samsung Galaxy S2 connected via Wifi as test device ***

Recursion_Test.aia
Fibonacci.jpg

Sven Wiebus

unread,
Oct 17, 2014, 7:16:48 PM10/17/14
to mitappinv...@googlegroups.com
Sorry folk,

something went wrong with the screen shot.in the if-statement. Attached is the real one ...

Sven
Fibonacci.jpg

Sven Wiebus

unread,
Oct 17, 2014, 7:31:20 PM10/17/14
to mitappinv...@googlegroups.com
... and yes, the procedure should be named like what she tries to implement, the calculation of the factorial of an integer, rather than "Fibonacci". This was caused by first trying the Fibonacci sequence.as a test. Maybe I should consider to not post code snippets again past 1 am in the morning anymore. ...

Abraham Getzler

unread,
Oct 18, 2014, 7:55:36 PM10/18/14
to mitappinv...@googlegroups.com
So you realize by now that your blocks were calculating
F(x) = F((x-1)*x) ?

You can get a clearer view of your nesting by right-click-External-Inputs
on them.

ABG

Sven Wiebus

unread,
Oct 21, 2014, 8:17:48 PM10/21/14
to mitappinv...@googlegroups.com
No, I actually did not realize that my example blocks calculated F(x) = F((x-1)*x) rather than F(x) = F(x-1) * x. Thanks for your advice!

This wrong bracketing amplified the occurrence of the issue. In AI2 Companion I found that calling F(x) with x>9 triggers the runtime error on the AI2 website while F(x) with 0 <= x <= 9 works fine as expected.

Installed on my handy as a build application the recursion works for x<32 while for >=32 a runtime error on the device is issued, what might be caused by a math overrun.

Recursion itself however seems to work!

Thanks again Abraham!
Reply all
Reply to author
Forward
0 new messages