[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A question about recursive programming

Hank Gong

12/12/2005 3:15:00 PM

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?
22 Answers

Stephen Waits

12/12/2005 3:19:00 PM

0


On Dec 12, 2005, at 7:14 AM, Hank Gong wrote:

> Can anyone give me some advice?

Do you just want to learn about recursion? Or are you interested in
solving this specific problem?

--Steve



Nobuyoshi Nakada

12/12/2005 3:33:00 PM

0

Hi,

At Tue, 13 Dec 2005 00:14:36 +0900,
Hank Gong wrote in [ruby-talk:170244]:
> def all_sum(arr)
> b=arr if arr.length==1

This line actually does nothing. You may want to write:

return arr if arr.length==1
?

--
Nobu Nakada


sanha lee

12/12/2005 3:58:00 PM

0

I think that it is better solution

def all_sum(arr)
return arr if arr.length == 1
temp = arr[-1]
return all_sum(arr[0...-1]) + all_sum(arr[1...-1]).collect {|i| i + temp}
end


On 12/12/05, Hank Gong <hankgong@gmail.com> 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?
>
>

Hank Gong

12/12/2005 4:05:00 PM

0

Because I read some other lauguage said that recursive programming can
totally replace loop structure.
So I want to think the loop as the recursive way!


On 12/13/05, Stephen Waits <steve@waits.net> wrote:
>
>
> On Dec 12, 2005, at 7:14 AM, Hank Gong wrote:
>
> > Can anyone give me some advice?
>
> Do you just want to learn about recursion? Or are you interested in
> solving this specific problem?
>
> --Steve
>
>
>

dblack

12/12/2005 4:22:00 PM

0

sanha lee

12/12/2005 4:46:00 PM

0

def all_sum(arr)
> return arr if arr.length == 1
> temp = arr[-1]
# sorry about this, it should be return all_sum(arr[0...-1]) +
all_sum(arr[0...-1]).collect .....
# not all_sum(arr[1...-1]).collect.
> return all_sum(arr[0...-1]) + all_sum(arr[1...-1]).collect {|i| i + temp}
> end

except that mistake, I can not see any difference between your code and
mine,
except that your code doesn't deal with the case where arr.length == 1.
my result of all_sum([1, 2, 3, 4]) is this [1, 2, 3, 4, 6, 5, 7, 8, 10]
sorry if i am wrong, no offence.




On 12/12/05, dblack@wobblini.net <dblack@wobblini.net> wrote:
>
> Hi --
>
> On Tue, 13 Dec 2005, sanha lee wrote:
>
> > I think that it is better solution
> >
> > def all_sum(arr)
> > return arr if arr.length == 1
> > temp = arr[-1]
> > return all_sum(arr[0...-1]) + all_sum(arr[1...-1]).collect {|i| i +
> temp}
> > end
>
> I get a too-deep recursion with that too. Here's another candidate:
>
> def all_sum(arr)
> case arr.size
> when 1 then []
> else arr[0..-2].map {|i| i + arr[-1]} + all_sum(arr[0..-2])
> end
> end
>
> p all_sum([1,2,3,4])
> => [5, 6, 7, 4, 5, 3]
>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
> "Ruby for Rails", forthcoming from Manning Publications, April 2006!
>
>

dblack

12/12/2005 4:56:00 PM

0

Robert Klemme

12/12/2005 5:47:00 PM

0

Hank Gong wrote:
> Because I read some other lauguage said that recursive programming can
> totally replace loop structure.
> So I want to think the loop as the recursive way!

IMHO Ruby is not well suited for that because you'll run into stack
limitations quite often. You probably better use functional languages for
that. They are usually much better at recursion.

Kind regards

robert

Hank Gong

12/12/2005 5:52:00 PM

0

Christian Neukirchen

12/12/2005 6:16:00 PM

0

Hank Gong <hankgong@gmail.com> writes:

> Because I read some other lauguage said that recursive programming can
> totally replace loop structure.
> So I want to think the loop as the recursive way!

Unfortunately, Ruby doesn't have tail-call optimization, so recursive
methods always are likely to overflow the stack.
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...