[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Recursion in Ruby

Redson

5/28/2006 8:12:00 PM

I'm learning ruby on my own and going through the normal academic
examples to try and get started in this language.

While trying to do the simple "powerset" problem that's so easy in
lisp/ml, I run into either a "stack too deep" or an outright segfault,
which I didn't expect from ruby. I haven't been able to find anything
on google about it. I'm using ruby 1.8.4 on linux x86.

with the following code:

class Array
def powerset
if !self
return []
end
hd,*tl = self
tl.powerset.each do |i|
i + (i.push(hd))
end
end
end

I get:
irb(main):001:0> require 'setops.rb'
=> true
irb(main):002:0> [1,2].powerset
SystemStackError: stack level too deep
from ./setops.rb:30:in `powerset'
from ./setops.rb:30:in `powerset'
from (irb):2

5 Answers

Tassilo Horn

5/28/2006 8:22:00 PM

0

"Redson" <brian.wagner@gmail.com> writes:

Hi Brian,

> class Array
> def powerset
> if !self
> return []
> end
> hd,*tl = self
> tl.powerset.each do |i|
> i + (i.push(hd))
> end
> end
> end

self will never be false or nil. If the instance was nil, you couldn't
call a instance method. ;-)

I think you want something like: if self.empty?

Bye,
Tassilo
--
A child of five could understand this! Fetch me a child of five!

Robert Klemme

5/28/2006 8:24:00 PM

0

Redson wrote:
> I'm learning ruby on my own and going through the normal academic
> examples to try and get started in this language.
>
> While trying to do the simple "powerset" problem that's so easy in
> lisp/ml, I run into either a "stack too deep" or an outright segfault,
> which I didn't expect from ruby. I haven't been able to find anything
> on google about it. I'm using ruby 1.8.4 on linux x86.
>
> with the following code:
>
> class Array
> def powerset
> if !self
> return []
> end
> hd,*tl = self
> tl.powerset.each do |i|
> i + (i.push(hd))
> end
> end
> end
>
> I get:
> irb(main):001:0> require 'setops.rb'
> => true
> irb(main):002:0> [1,2].powerset
> SystemStackError: stack level too deep
> from ./setops.rb:30:in `powerset'
> from ./setops.rb:30:in `powerset'
> from (irb):2
>

Your recursion never terminates because !self is never true. Only false
and nil are treated as false all other values (including empty arrays)
are true in a boolean context. Also I'm not sure whether your method
has a proper return value.

Regards

robert


Robert Klemme

5/29/2006 7:16:00 AM

0

Tassilo Horn wrote:
> "Redson" <brian.wagner@gmail.com> writes:
>
> Hi Brian,
>
>> class Array
>> def powerset
>> if !self
>> return []
>> end
>> hd,*tl = self
>> tl.powerset.each do |i|
>> i + (i.push(hd))
>> end
>> end
>> end
>
> self will never be false or nil. If the instance was nil, you couldn't
> call a instance method. ;-)

That's not exactly true: nil has a lot of methods you can invoke

>> nil.to_i
=> 0
>> nil.nil?
=> true
>> nil.to_s
=> ""
>> nil.class
=> NilClass

You can even define your own methods:

>> class NilClass
>> def my_meth() "foo" end
>> end
=> nil
>> nil.my_meth
=> "foo"

> I think you want something like: if self.empty?

empty? is sufficient.

Kind regards

robert

Tassilo Horn

5/29/2006 8:25:00 AM

0

Robert Klemme <bob.news@gmx.net> writes:

Hi Robert,

>> self will never be false or nil. If the instance was nil, you
>> couldn't call a instance method. ;-)
>
> That's not exactly true: nil has a lot of methods you can invoke

It's not even "not exactly true" -- it's simply wrong. I had to do some
Java-stuff when I replied. Shame on me. :-)

>> I think you want something like: if self.empty?
>
> empty? is sufficient.

Jepp.

Bye,
Tassilo
--
A child of five could understand this! Fetch me a child of five!

Robert Klemme

5/29/2006 9:27:00 AM

0

Tassilo Horn wrote:
> Robert Klemme <bob.news@gmx.net> writes:
>
> Hi Robert,
>
>>> self will never be false or nil. If the instance was nil, you
>>> couldn't call a instance method. ;-)
>> That's not exactly true: nil has a lot of methods you can invoke
>
> It's not even "not exactly true" -- it's simply wrong. I had to do some
> Java-stuff when I replied. Shame on me. :-)

*ggg*

I was trying to be polite...

Kind regards

robert