[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

The Man or Boy Recursion Test

Werner

11/10/2007 3:29:00 PM

Hello,

being new to ruby I try to understand the language. In a different
context I came across the following:

http://en.wikipedia.org/wiki/Man_o...

Out of curiosity I tried to write an equivalent piece of code in ruby
but apparently my depth of knowledge is not good enough.

Is there anyone who could enlighten me?

Thanks and Best Regards

Werner Dahn
16 Answers

Tim Hunter

11/10/2007 9:18:00 PM

0

Werner wrote:
> Hello,
>
> being new to ruby I try to understand the language. In a different
> context I came across the following:
>
> http://en.wikipedia.org/wiki/Man_o...
>
> Out of curiosity I tried to write an equivalent piece of code in ruby
> but apparently my depth of knowledge is not good enough.
>
> Is there anyone who could enlighten me?
>
> Thanks and Best Regards
>
> Werner Dahn
>

# Man or boy test
def a(k, x1, x2, x3, x4, x5)
b ||= Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }
return k <= 0 ? x4[] + x5[] : b[]
end

puts a(10, Proc.new {1}, Proc.new {-1}, Proc.new {-1}, Proc.new {1},
Proc.new {0})


--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Lloyd Linklater

11/10/2007 11:43:00 PM

0

Tim Hunter wrote:
> Werner wrote:
>> Is there anyone who could enlighten me?
>>
>> Thanks and Best Regards
>>
>> Werner Dahn
>>
>
> # Man or boy test
> def a(k, x1, x2, x3, x4, x5)
> b ||= Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }
> return k <= 0 ? x4[] + x5[] : b[]
> end
>
> puts a(10, Proc.new {1}, Proc.new {-1}, Proc.new {-1}, Proc.new {1},
> Proc.new {0})

Nice post, Tim. I tested it and, when it gave the correct answer, I
decided to share it with the world.

http://en.wikipedia.org/wiki/Man_o...
--
Posted via http://www.ruby-....

Werner

11/11/2007 3:06:00 AM

0

> # Man or boy test
> def a(k, x1, x2, x3, x4, x5)
> b ||= Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }
> return k <= 0 ? x4[] + x5[] : b[]
> end
>
> puts a(10, Proc.new {1}, Proc.new {-1}, Proc.new {-1}, Proc.new {1},
> Proc.new {0})
>
Thank you very much! I am impressed.
Best Regards
Werner Dahn

Tim Hunter

11/11/2007 2:21:00 PM

0

Lloyd Linklater wrote:
> Tim Hunter wrote:
>> Werner wrote:
>>> Is there anyone who could enlighten me?
>>>
>>> Thanks and Best Regards
>>>
>>> Werner Dahn
>>>
>> # Man or boy test
>> def a(k, x1, x2, x3, x4, x5)
>> b ||= Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }
>> return k <= 0 ? x4[] + x5[] : b[]
>> end
>>
>> puts a(10, Proc.new {1}, Proc.new {-1}, Proc.new {-1}, Proc.new {1},
>> Proc.new {0})
>
> Nice post, Tim. I tested it and, when it gave the correct answer, I
> decided to share it with the world.
>
> http://en.wikipedia.org/wiki/Man_o...

Sweet!

I realized last night that the ||= is superfluous. A simple assignment
is enough.

b = Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }


--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Rick DeNatale

11/11/2007 2:59:00 PM

0

On Nov 11, 2007 9:21 AM, Tim Hunter <TimHunter@nc.rr.com> wrote:
>
> I realized last night that the ||= is superfluous. A simple assignment
> is enough.
>
> b = Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }

Right, because b will always be nil here, it's a local variable so
there's a new b for each resursion. Otherwise it would kind of defeat
what the Man Or Boy example is trying to illustrate.

And it can be made a little shorter, and more computer sciency <G>, by
replacing all those "Proc.new"s with "lambda"s.

# Man or boy test
def a(k, x1, x2, x3, x4, x5)
b = lambda { k -= 1; a(k, b, x1, x2, x3, x4) }
return k <= 0 ? x4[] + x5[] : b[]
end

puts a(10, lambda {1}, lambda {-1}, lambda {-1}, lambda {1}, lambda {0})


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Lloyd Linklater

11/11/2007 4:48:00 PM

0

Since I posted your ruby version, there have been more posts in C,
haskell, smalltalk. I sense that a snowball has started an avalanche!
:)
--
Posted via http://www.ruby-....

Tim Hunter

11/11/2007 8:19:00 PM

0

Lloyd Linklater wrote:
> Since I posted your ruby version, there have been more posts in C,
> haskell, smalltalk. I sense that a snowball has started an avalanche!
> :)

Well, the Ruby version certainly wins the Man-or-boy-test-golf contest.
But I've been programming in C for over 20 years and I can say with
authority that whoever did the C version has C-coding cojones the size
of hubcaps.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Morton Goldberg

11/11/2007 9:57:00 PM

0

On Nov 11, 2007, at 9:59 AM, Rick DeNatale wrote:

> On Nov 11, 2007 9:21 AM, Tim Hunter <TimHunter@nc.rr.com> wrote:
>>
>> I realized last night that the ||= is superfluous. A simple
>> assignment
>> is enough.
>>
>> b = Proc.new { k -= 1; a(k, b, x1, x2, x3, x4) }
>
> Right, because b will always be nil here, it's a local variable so
> there's a new b for each resursion. Otherwise it would kind of defeat
> what the Man Or Boy example is trying to illustrate.
>
> And it can be made a little shorter, and more computer sciency <G>, by
> replacing all those "Proc.new"s with "lambda"s.
>
> # Man or boy test
> def a(k, x1, x2, x3, x4, x5)
> b = lambda { k -= 1; a(k, b, x1, x2, x3, x4) }
> return k <= 0 ? x4[] + x5[] : b[]
> end
>
> puts a(10, lambda {1}, lambda {-1}, lambda {-1}, lambda {1}, lambda
> {0})

Surely we can eliminate the 'return' too and make it even shorter:

def a(k, x1, x2, x3, x4, x5)
b = lambda { k -= 1; a(k, b, x1, x2, x3, x4) }
k <= 0 ? x4[] + x5[] : b[]
end

puts a(10, lambda {1}, lambda {-1}, lambda {-1}, lambda {1}, lambda {0})

Regards, Morton

Rick DeNatale

11/11/2007 11:24:00 PM

0

On Nov 11, 2007 3:18 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:

> Well, the Ruby version certainly wins the Man-or-boy-test-golf contest.
> But I've been programming in C for over 20 years and I can say with
> authority that whoever did the C version has C-coding cojones the size
> of hubcaps.

I was surprised and amused to find that there was already an PL/I
implementation in the wikipedia article.

A much (unnecessarily) maligned language deep in my past.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Tim Hunter

11/11/2007 11:42:00 PM

0

Rick DeNatale wrote:
> On Nov 11, 2007 3:18 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:
>
>> Well, the Ruby version certainly wins the Man-or-boy-test-golf contest.
>> But I've been programming in C for over 20 years and I can say with
>> authority that whoever did the C version has C-coding cojones the size
>> of hubcaps.
>
> I was surprised and amused to find that there was already an PL/I
> implementation in the wikipedia article.
>
> A much (unnecessarily) maligned language deep in my past.
>

Ummmm...before I wrote C I wrote PL/I. Not a lot, but enough to be able
to use the PL/I version of the Man or Boy test to figure out how to code
the Ruby version.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]