Morton Goldberg
11/11/2007 9:57:00 PM
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