Dave Thomas <Dave / PragmaticProgrammer.com> wrote in message news:<m2u1l69bl6.fsf / zip.local.thomases.com>...

> We discuss the 'once' trick on page 251, but that doesn't vary the
> result depending on the parameter passed in. You could try extending
> that, or you could have a look at Robert Feldt's memoize package:

   I've been having a tough time getting this to work.  Can someone
tell me what I'm doing wrong?  Here is an attempt to memoize a clumsy
recursion that screams for memoization (a recursive calculation of the
binomial coefficient "n choose k").

irb(main):002:0> class Integer
irb(main):003:1>   def choose(k)
irb(main):004:2>     if (k==0 or k==self)
irb(main):005:3>       1
irb(main):006:3>     else
                         (self-1).choose(k) + (self-1).choose(k-1)
irb(main):009:3>       end
irb(main):010:2>     end
irb(main):011:1>   end
nil
irb(main):012:0> 15 .choose 7
6435
irb(main):013:0> class Integer
irb(main):014:1>   memoize :choose
irb(main):015:1>   end
[:choose]
irb(main):016:0> 15 .choose 7
57
   
   Oops!  I'm one confused piggy.

              Regards, Bret

oinkoink+unet / rexx.com
http://www.rexx.com/~oinkoink