I have a question about using a recursive function in a module. I made the following example to illustrate the question.
The followwing code works:
Clear[fib]
fib[1] = 1;
fib[2] = 1;
fib[n_] := fib[n - 1] + fib[n - 2]
f[a_, b_, n_] := Module[{x, y},
fib[1] = a;
fib[2] = b;
x = fib[n];
Print[x];
fib[1] = 2 a;
fib[2] = 2 b;
y = fib[n];
Print[y];
x + y
]
f[1, 1, 3] = 6.
but is slow, I want to use memoization, i.e.
fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]
but in that case
f[1, 1, 3] = 4.
Question: How can I use a recursive function in a Module?
fib[a_,b_,n_]
more appropriate? $\endgroup$ – Dr. belisarius Nov 11 '13 at 14:47