cirfu
9/13/2008 8:05:00 PM
Im using ruby 1.8.6 mri. and it seems to optimize tailcalls which I
have been told it doesn't. what else could be going on here?
###################
def factail(n, acc=1)
if n > 0 then factail(n-1, n*acc)
else acc end
end
irb(main):163:0> factail(10000)
factail(10000)
284625968091705451890641321 and so on...
###################
def facto(n)
if n > 0 then n * facto(n-1)
else 1 end
end
irb(main):164:0> facto(10000)
facto(10000)
SystemStackError: stack level too deep
from (irb):155:in `facto'
from (irb):155:in `facto'
from (irb):158
from :0
irb(main):165:0>
########################
def fact(n)
def f(n, acc)
if n > 0
then f(n-1, n*acc)
else acc
end
end
f(n, 1)
end
irb(main):183:0> fact(10000)
fact(10000)
2846259680917054518 ...
also works.