[Prev][Next][Index][Thread]
Re: Dynamic typing confuses me!
A just-for-fun definition using methods for memoization:
define open method fact(x :: <integer>) => (f :: <integer>)
local method do-fact(x :: <integer>) => (f :: <integer>)
if(x == 1)
1
else
do-fact(x - 1) * x
end;
end;
let result :: <integer> = do-fact(x);
add-method(fact,
method(a == x) => (f :: <integer>)
result
end);
result;
end;
Calling fact(5) the first time does the calculation and adds a method
to the generic that returns the result without calculating it
again. Calling the generic will then call that new method.
Chris.
--
http://www.double.co.nz/dylan
Follow-Ups:
References: