Differences From Artifact [5b2c3c452aca5b18]:
- File
sample/fib.pmy
- 2010-11-24 12:14:00 - part of checkin [3ae09b8cbf] on branch trunk - changed if-then-else syntax (user: kinaba) [annotate]
To Artifact [4438bc82733dcb16]:
- File
sample/fib.pmy
- 2010-11-27 11:46:26 - part of checkin [203e4cb208] on branch trunk - fixed automatic memoization bug (whole part of the contexts are now used as the memo key) (user: kinaba) [annotate]
1 +# Standard Fibonacci function
1 2 def fib(x)
2 3 {
3 4 if x<2 then 1 else fib(x-1) + fib(x-2)
4 5 };
5 6
6 7 let upto = fun(n, f){
7 8 if n > 0: upto(n-1,f);
8 9 f(n)
9 10 };
10 11
11 12 var compose = fun(f,g){ fun(x){f(g(x))} };
12 13 var "<<" = compose;
13 14
14 -upto(16, print<<fib);
15 +upto(18, print<<fib);
16 +
17 +
18 +######################################
19 +# Omake. Feel the automatic memoization!
20 +
21 +# Set up a mirror layer (do-the-same-thing layer) of @value
22 +@@m(x){x};
23 +@m "+" (x,y) { @value(@m(x)+@m(y)) };
24 +@m "-" (x,y) { @value(@m(x)-@m(y)) };
25 +@m "<" (x,y) { @value(@m(x)<@m(y)) };
26 +@m "if" (c,t,e) { @value(if @m(c) then @m(t()) else @m(e())) };
27 +
28 +# Helper function to call fib in layer @m
29 +def fibm(x @value) { @m(fib(@value(x))) };
30 +
31 +# User defined layers are automatically memoized.
32 +# So this is much faster.
33 +upto(18, print<<fibm);