Yes, it makes sense.

fib0 n  is doubly recursive with no help from the system
whatsoever, so is exponential in n .

fib0m n is also doubly recursive with memoization.
However, the memo-ed function "sees" mostly
2-element vector arguments and the current implementation
of M. only retains information on integer atom arguments.
Try
   fib0ma=: 3 : 'if. y<2 do. 1 else. +/fib0ma y-1 2 end.'"0 @(1!:2&2) M.
   fib0ma 7

fib1m has all the right conditions for M. to effect an
exponential improvement.  The following definition
is similarly efficient:
   fib2m=: 3 : 'if. y<2 do. 1 else. +/fib2m y-1 2 end.' M."0



----- Original Message -----
From: Devon McCormick <[email protected]>
Date: Wednesday, June 24, 2009 9:07
Subject: [Jprogramming] Defining function on scalar defeats memoization - 
should it?
To: J-programming forum <[email protected]>

> Does this make sense?
> 
>    fib0 =:   3 : 'if. y<2 do. 1 else. 
> +/fib0        y-1 2 end.'"0
>    fib0m=: 3 : 'if. y<2 do. 1 else. 
> +/fib0m      y-1 2 end.'"0 M.
>    fib1m=: 3 : 'if. y<2 do. 1 else. +/fib1m"0 ] y-1 
> 2 end.' M.
> 
>    6!:2 'fib0 i.30'
> 18.99775
>    6!:2 'fib0m i.30'
> 20.822316
>    6!:2 'fib1m"0 ] i.30'
> 0.00044411823
>    (fib1m"0 ] i.30)-:fib0m i.30
> 1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to