For some reason Ruby seems to perform better when facing left recursion. For example:
def left_recursive_factorial(number)
return 1 if number.zero?
left_recursive_factorial(number.pred) * number
end
def right_recursive_factorial(number)
return 1 if number.zero?
number * right_recursive_factorial(number.pred)
end
When I call these methods with a value over 9000 () I get different results:
left_recursive_factorial(9001)
# => factorial of 9001
right_recursive_factorial(9001)
# => SystemStackError: stack level too deep
# from (pry):6:in `right_recursive_factorial'
I couldn't find any explanation for this behavior.
The only thing that seemed somewhat related was about LL()
parsers having problems with left recursion and I guess you can flip it around, but I haven't dug much into it.
Could someone explain in a little more detail what causes left and right recursions to perform differently (generally and in Ruby specifically) and if you have the possibility to choose one or the other why would you pick it (and why was left chosen in Ruby)?