From: William James Date: 2005-12-13T15:47:38+09:00 Subject: Re: A question about recursive programming Hank Gong wrote: > I want to calculate all sum possibility of interger array. I know there are > other non-recursive way to do it. > But when I wrote recursive code to achieve it, I just got error. > > > def all_sum(arr) > b=arr if arr.length==1 > 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? class Array def add( n ) self.map{|item| item + n } end def tail self[1..-1] end end def sums( array ) return [] if array.size < 2 array.tail.add( array.first ) + sums( array.tail ) end