# Standard Fibonacci function
def fib(x)
{
	if x<2 then 1 else fib(x-1) + fib(x-2)
};

let upto = fun(n, f){
	if n > 0: upto(n-1,f);
	f(n)
};

var compose = fun(f,g){ fun(x){f(g(x))} };
var "<<" = compose;

upto(18, print<<fib);


######################################
# Omake. Feel the automatic memoization!

# Set up a mirror layer (do-the-same-thing layer) of @value
@@m(x){x};
@m "+" (x,y) { @value(@m(x)+@m(y)) };
@m "-" (x,y) { @value(@m(x)-@m(y)) };
@m "<" (x,y) { @value(@m(x)<@m(y)) };
@m "if" (c,t,e) { @value(if @m(c) then @m(t()) else @m(e())) };

# Helper function to call fib in layer @m
def fibm(x @value) { @m(fib(@value(x))) };

# User defined layers are automatically memoized.
# So this is much faster.
upto(18, print<<fibm);
