Can continuations be used as a replacement for recursion?
Posted
by Sam
on Stack Overflow
See other posts from Stack Overflow
or by Sam
Published on 2010-03-17T00:14:04Z
Indexed on
2010/03/17
0:21 UTC
Read the original article
Hit count: 509
The following function generates a 'stack level too deep (SystemStackError)' for n = 5,000
def factorial(n)
n == 0 ? 1 : factorial(n -1) * n
end
Is there a way to avoid this error using continuations/callcc?
Note:
I know this can be implemented without recursion. e.g.
def factorial2(n)
(1..n).inject(1) {|result, n| result * n }
end
© Stack Overflow or respective owner