Ruby Recursion

Hi all,

I am new to Ruby, and I have only known it through rails. I was
wondering if it's a good idea to use recursion sparingly in Ruby?
How efficiently is it implemented? Do recursive calls only store local
variables or the entire method?
Is there a maximum stack size/depth?

Thanks!

Bob

···

--
Posted via http://www.ruby-forum.com/.

I was wondering if it's a good idea to use recursion sparingly in Ruby?

My opinion is, yes, it is.

How efficiently is it implemented? Do recursive calls only store local
variables or the entire method?

I'm not aware of any recursive specific optimizations and method calls in general are fairly expensive in Ruby.

Is there a maximum stack size/depth?

Yes and Ruby uses the C stack so it's fairly easy to hit. I avoid recursion unless I am reasonably sure it will not go too deep.

James Edward Gray II

···

On May 7, 2006, at 12:34 PM, Bob wrote:

It's a good idea to recursion as much as you find convenient, and then test your code and see if it's too slow or not. If it's too slow, you have to figure out why, and you can put recursion on a long list of possible reasons.

If you have a specific algorithm you are considering doing recursively, you could easily test just that algorithm to see if it's fast enough.

-- Elliot Temple

···

On May 7, 2006, at 10:34 AM, Bob wrote:

Hi all,

I am new to Ruby, and I have only known it through rails. I was
wondering if it's a good idea to use recursion sparingly in Ruby?
How efficiently is it implemented?

Does anyone know if Ruby can optimize tail-recursions?

···

On 5/7/06, Bob <papersmith@gmail.com> wrote:

Hi all,

I am new to Ruby, and I have only known it through rails. I was
wondering if it's a good idea to use recursion sparingly in Ruby?
How efficiently is it implemented? Do recursive calls only store local
variables or the entire method?
Is there a maximum stack size/depth?

Thanks!

Bob

--
Posted via http://www.ruby-forum.com/\.

I'm more on James' side: since recursion doesn't really scale in Ruby
I try to avoid it where possible from the start.

13:16:04 [~]: ruby -e 'def r(i)puts i;r i+1 end; r 0'|tail
-e:1:in `r': stack level too deep (SystemStackError)
        from -e:1:in `r'
        from -e:1
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
13:16:06 [~]:

Kind regards

robert

···

2006/5/8, Elliot Temple <curi@curi.us>:

On May 7, 2006, at 10:34 AM, Bob wrote:

> Hi all,
>
> I am new to Ruby, and I have only known it through rails. I was
> wondering if it's a good idea to use recursion sparingly in Ruby?
> How efficiently is it implemented?

It's a good idea to recursion as much as you find convenient, and
then test your code and see if it's too slow or not. If it's too
slow, you have to figure out why, and you can put recursion on a long
list of possible reasons.

--
Have a look: Robert K. | Flickr

Not yet. I believe this is planned for YARV though, if it doesn't already do it.

James Edward Gray II

···

On May 8, 2006, at 8:44 PM, Francis Cianfrocca wrote:

Does anyone know if Ruby can optimize tail-recursions?

Not yet, though I think YARV[1] can do it.

Ryan

1. http://www.atdot.net/yarv/

···

On 5/8/06, Francis Cianfrocca <garbagecat10@gmail.com> wrote:

Does anyone know if Ruby can optimize tail-recursions?

I'm going to weigh in on Elliot's side =) Recursive solutions for all
their sluggishness are often the most elegent and easiest to read.

For what it's worth I would recomend:

1) Write your test.
2) Make it work in the dumbest possible way.
3) Profile.
4) Optimize as necessary.

"Premature optimization is the root of all evil." -- C. A. R. Hoare

···

--
Lou

Great minds think alike. You only beat me by 1 minute :wink:

Ryan

···

On 5/8/06, James Edward Gray II <james@grayproductions.net> wrote:

Not yet. I believe this is planned for YARV though, if it doesn't
already do it.

Hi,

I found this article today :slight_smile:

James Edward Gray II wrote:

Not yet. I believe this is planned for YARV though, if it doesn't
already do it.

YARV can achieve tail *call* optimization (not on default). But not
tail *recursion*.

On Ruby syntax, we can't optimize tail recursion. Because Ruby don't
support like "Java private method".

How about this pattern? :

···

---------------------------------
class C
  def fact n
    n <= 2 ? 1 : n * fact(n)
  end
end

class D < C
  def fact n
    p n
    super
  end
end

D.new.fact(10)

or Module#include, Object#extend, ....

Regards,
--
// SASADA Koichi at atdot dot net

I like recursion, and it's very unfortunate that Ruby's recursion is pretty crappy. I say use it initially, then go to a loop-based implementation if you overflow the stack or find that it's too slow. I end up doing this about 10% of the time that I use recursion.

- Jake McArthur