Differences From 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]
To Artifact [fd8a6f88bc7a9f4a]:
- File
sample/fib.pmy
- 2010-12-02 10:30:24 - part of checkin [88827b8336] on branch trunk - sample clean-up (user: kinaba) [annotate]
6 6
7 let upto = fun(n, f){ 7 let upto = fun(n, f){
8 if n > 0: upto(n-1,f); 8 if n > 0: upto(n-1,f);
9 f(n) 9 f(n)
10 }; 10 };
11 11
12 var compose = fun(f,g){ fun(x){f(g(x))} }; 12 var compose = fun(f,g){ fun(x){f(g(x))} };
13 var "<<" = compose; | 13 var << = compose;
14 14
15 upto(18, print<<fib); 15 upto(18, print<<fib);
16 16
17 17
18 ###################################### 18 ######################################
19 # Omake. Feel the automatic memoization! 19 # Omake. Feel the automatic memoization!
20 20
21 # Set up a mirror layer (do-the-same-thing layer) of @value 21 # Set up a mirror layer (do-the-same-thing layer) of @value
22 @@m(x){x}; 22 @@m(x){x};
23 @m "+" (x,y) { @value(@m(x)+@m(y)) }; | 23 @m + (x,y) { @value(@m(x)+@m(y)) };
24 @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)) }; | 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())) }; | 26 @m if (c,t,e) { @value(if @m(c) then @m(t()) else @m(e())) };
27 27
28 # Helper function to call fib in layer @m 28 # Helper function to call fib in layer @m
29 def fibm(x @value) { @m(fib(@value(x))) }; 29 def fibm(x @value) { @m(fib(@value(x))) };
30 30
31 # User defined layers are automatically memoized. 31 # User defined layers are automatically memoized.
32 # So this is much faster. 32 # So this is much faster.
33 upto(18, print<<fibm); 33 upto(18, print<<fibm);