Artifact Content
Not logged in

Artifact fd8a6f88bc7a9f4ad60cbd8ec0c6c9d4b82cbd4f


     1  # Standard Fibonacci function
     2  def fib(x)
     3  {
     4  	if x<2 then 1 else fib(x-1) + fib(x-2)
     5  };
     6  
     7  let upto = fun(n, f){
     8  	if n > 0: upto(n-1,f);
     9  	f(n)
    10  };
    11  
    12  var compose = fun(f,g){ fun(x){f(g(x))} };
    13  var << = compose;
    14  
    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);