Forum: Ruby Re: A question about recursive programming

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2005-12-12 16:45
(Received via mailing list)
>     temp=arr.delete_at(arr.length-1)
>     b=all_sum(arr)+all_sum(arr).collect{|i| i+temp}
> end
>
> c=[1,2]
> p all_sum(c)
>
> Error: in `all_sum': stack level too deep (SystemStackError)
>
> Can anyone give me some advice?

Hmm,

the b= lines doesn't make much sense, you may want to replace
them with return statemants.

Note that delete_at modifies the receiver - the original array
(for this and all other recursions). So it won't work even if
you change the b= to return.

cheers

Simon
This topic is locked and can not be replied to.