Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The Man or Boy Recursion Test

30 views
Skip to first unread message

Werner

unread,
Nov 10, 2007, 10:28:45 AM11/10/07
to
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_or_boy_test

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

Tim Hunter

unread,
Nov 10, 2007, 4:17:51 PM11/10/07
to

# 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/projects/rmagick/]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]

Lloyd Linklater

unread,
Nov 10, 2007, 6:43:12 PM11/10/07
to
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_or_boy_test
--
Posted via http://www.ruby-forum.com/.

Werner

unread,
Nov 10, 2007, 10:05:44 PM11/10/07
to
> # 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

unread,
Nov 11, 2007, 9:21:16 AM11/11/07
to

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) }

Rick DeNatale

unread,
Nov 11, 2007, 9:59:06 AM11/11/07
to
On Nov 11, 2007 9:21 AM, Tim Hunter <TimH...@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.denhaven2.com/

Lloyd Linklater

unread,
Nov 11, 2007, 11:48:21 AM11/11/07
to
Since I posted your ruby version, there have been more posts in C,
haskell, smalltalk. I sense that a snowball has started an avalanche!
:)

Tim Hunter

unread,
Nov 11, 2007, 3:18:30 PM11/11/07
to
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.

Morton Goldberg

unread,
Nov 11, 2007, 4:57:17 PM11/11/07
to

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

unread,
Nov 11, 2007, 6:23:58 PM11/11/07
to
On Nov 11, 2007 3:18 PM, Tim Hunter <TimH...@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.

Tim Hunter

unread,
Nov 11, 2007, 6:42:26 PM11/11/07
to
Rick DeNatale wrote:
> On Nov 11, 2007 3:18 PM, Tim Hunter <TimH...@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.

Rick DeNatale

unread,
Nov 11, 2007, 7:10:15 PM11/11/07
to
On Nov 11, 2007 6:42 PM, Tim Hunter <TimH...@nc.rr.com> wrote:
> Rick DeNatale wrote:
> > On Nov 11, 2007 3:18 PM, Tim Hunter <TimH...@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.

Sometimes I feel my age.

Last week I gave a talk to the local "agile" meetup group about "A
History of Agility" When I introduced myself I said that I started
programming in College in 1970. Out of the 15-20 people in the
audience, only about 3-4 were born then, some by only a few months.

Morton Goldberg

unread,
Nov 11, 2007, 7:22:24 PM11/11/07
to
On Nov 11, 2007, at 6:42 PM, Tim Hunter wrote:

> 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.

Interesting. I used your Ruby version to figure out how to code it in
Mathematica. I was able to make a fairly direct translation.

<code>
$RecursionLimit = 1665; (* anything less fails for k0 = 10 *)

a[k0_, x1_, x2_, x3_, x4_, x5_] := Module[{k, b },
k = k0;
b = (k--; a[k, b, x1, x2, x3, x4]) &;
If[k <= 0, x4[] + x5[], b[]]]

a[10, 1 &, -1 &, -1 &, 1 &, 0 &] (* => -67 *)
</code>

Note how compact the Mathematica notation for the thunks is.

Regards, Morton

Tim Hunter

unread,
Nov 11, 2007, 8:46:20 PM11/11/07
to

Another one for Wikipedia. I guess we owe Werner (the OP) a big "thanks"
for introducing such an interesting thread. Thanks, Werner!

Lloyd Linklater

unread,
Nov 11, 2007, 9:50:34 PM11/11/07
to
Morton,

Your post did not have the formatting that the other languages did. I
added that for you so the other kids would not make fun of your code.
:) I did not use any language coloring as the wiki does not recognize
mathmatica.

I hope that you do not mind.

Morton Goldberg

unread,
Nov 11, 2007, 10:26:52 PM11/11/07
to
On Nov 11, 2007, at 8:46 PM, Tim Hunter wrote:

> Morton Goldberg wrote:
>> On Nov 11, 2007, at 6:42 PM, Tim Hunter wrote:
>>> 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.
>> Interesting. I used your Ruby version to figure out how to code it
>> in Mathematica. I was able to make a fairly direct translation.
>> <code>
>> $RecursionLimit = 1665; (* anything less fails for k0 = 10 *)
>> a[k0_, x1_, x2_, x3_, x4_, x5_] := Module[{k, b },
>> k = k0;
>> b = (k--; a[k, b, x1, x2, x3, x4]) &;
>> If[k <= 0, x4[] + x5[], b[]]]
>> a[10, 1 &, -1 &, -1 &, 1 &, 0 &] (* => -67 *)
>> </code>
>> Note how compact the Mathematica notation for the thunks is.
>> Regards, Morton
>
> Another one for Wikipedia.

I've posted it there. I like the Smalltalk example also -- very
concise yet still readable.

> I guess we owe Werner (the OP) a big "thanks" for introducing such
> an interesting thread. Thanks, Werner!

Indeed.

Regards, Morton

Morton Goldberg

unread,
Nov 11, 2007, 10:37:59 PM11/11/07
to
On Nov 11, 2007, at 9:50 PM, Lloyd Linklater wrote:

> Morton,
>
> Your post did not have the formatting that the other languages
> did. I
> added that for you so the other kids would not make fun of your code.
> :) I did not use any language coloring as the wiki does not recognize
> mathmatica.

I'm glad you fixed it up. That was my first post on Wikipedia. I
tried <source lang="mathematica">, but as you pointed out, that
doesn't work. I didn't know that <source lang="text"> was the best
alternative. But I do now :). Thanks.

Regards, Morton

0 new messages