Code: Select all
on mouseUp
answer factorial(5)
end mouseUp
function factorial var
if var = 0 then return 1
else return var * factorial(var - 1)
end factorial
Craig Newman
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Code: Select all
on mouseUp
answer factorial(5)
end mouseUp
function factorial var
if var = 0 then return 1
else return var * factorial(var - 1)
end factorial
Code: Select all
function factorial 5
if 5 = 0 then return 1
else return 5 * factorial(5 - 1)
end factorial
Code: Select all
function factorial 4
if 4 = 0 then return 1
else return 4 * factorial(4 - 1)
end factorial
Code: Select all
function factorial 3
if 3 = 0 then return 1
else return 3 * factorial(3 - 1)
end factorial
Hi Craig,dunbarx wrote:I am intrigued by how LC stores the running sums.
Obviously it does, somewhere. But where?
Code: Select all
local tmp
local _increment
on mouseUp
put empty into tmp
put empty into _increment
answer factorial( 5)
put tmp
end mouseUp
function factorial var
local _return, _step
add 1 to _increment
put _increment into _step
if var = 1 then
put 1 into _return
followReturns _increment, _step, _return
return _return
else
followReturns _increment, _step, _return
put var * factorial(var - 1) into _return
followReturns _increment, _step, _return
return _return
end if
end factorial
on followReturns i, s, r
put "increment: " & i & " step: " & s & " returns: " & r & cr after tmp
end followReturns
Craig,dunbarx wrote:Thierry.
I see how you have deconstructed and annotated (quite nicely) the recursion process. But YOU did that.
How does LC do that? Obviously, somehow. Inside and invisible, eh? Well, OK.
But I still am intrigued by how it knows to do that, since there is nothing "typing" a recursion handler as opposed to any other kind of handler. Does that make my quandary more clear?
Check for details: https://en.wikipedia.org/wiki/Call_stackIn computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack".