203e4cb208 2010-11-27 kinaba: # Standard Fibonacci function 68546f3e9f 2010-11-09 kinaba: def fib(x) 68546f3e9f 2010-11-09 kinaba: { 3ae09b8cbf 2010-11-24 kinaba: if x<2 then 1 else fib(x-1) + fib(x-2) 68546f3e9f 2010-11-09 kinaba: }; 68546f3e9f 2010-11-09 kinaba: 3ae09b8cbf 2010-11-24 kinaba: let upto = fun(n, f){ 3ae09b8cbf 2010-11-24 kinaba: if n > 0: upto(n-1,f); 68546f3e9f 2010-11-09 kinaba: f(n) 68546f3e9f 2010-11-09 kinaba: }; 68546f3e9f 2010-11-09 kinaba: 68546f3e9f 2010-11-09 kinaba: var compose = fun(f,g){ fun(x){f(g(x))} }; 68546f3e9f 2010-11-09 kinaba: var "<<" = compose; 68546f3e9f 2010-11-09 kinaba: 203e4cb208 2010-11-27 kinaba: upto(18, print<<fib); 203e4cb208 2010-11-27 kinaba: 203e4cb208 2010-11-27 kinaba: 203e4cb208 2010-11-27 kinaba: ###################################### 203e4cb208 2010-11-27 kinaba: # Omake. Feel the automatic memoization! 203e4cb208 2010-11-27 kinaba: 203e4cb208 2010-11-27 kinaba: # Set up a mirror layer (do-the-same-thing layer) of @value 203e4cb208 2010-11-27 kinaba: @@m(x){x}; 203e4cb208 2010-11-27 kinaba: @m "+" (x,y) { @value(@m(x)+@m(y)) }; 203e4cb208 2010-11-27 kinaba: @m "-" (x,y) { @value(@m(x)-@m(y)) }; 203e4cb208 2010-11-27 kinaba: @m "<" (x,y) { @value(@m(x)<@m(y)) }; 203e4cb208 2010-11-27 kinaba: @m "if" (c,t,e) { @value(if @m(c) then @m(t()) else @m(e())) }; 203e4cb208 2010-11-27 kinaba: 203e4cb208 2010-11-27 kinaba: # Helper function to call fib in layer @m 203e4cb208 2010-11-27 kinaba: def fibm(x @value) { @m(fib(@value(x))) }; 203e4cb208 2010-11-27 kinaba: 203e4cb208 2010-11-27 kinaba: # User defined layers are automatically memoized. 203e4cb208 2010-11-27 kinaba: # So this is much faster. 203e4cb208 2010-11-27 kinaba: upto(18, print<<fibm);