From: "Kroeger, Simon (ext)" Date: 2005-12-13T00:42:35+09:00 Subject: Re: A question about recursive programming > From: Hank Gong [mailto:hankgong@gmail.com] > > 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? 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