35 lines
1 KiB
Text
35 lines
1 KiB
Text
/////// Y-combinator function (for single-argument lambdas) ///////
|
|
|
|
y @FN [f]
|
|
{ @( x -> { @f (z -> {@(@x x) z}) } ) // output of this expression is treated as a verb, due to outer @( )
|
|
( x -> { @f (z -> {@(@x x) z}) } ) // this is the argument supplied to the above verb expression
|
|
};
|
|
|
|
|
|
/////// Function to generate an anonymous factorial function as the return value -- (not tail-recursive) ///////
|
|
|
|
fact_gen @FN [f]
|
|
{ n -> { (n<=0) ? {1} {n * (@f n-1)}
|
|
}
|
|
};
|
|
|
|
|
|
/////// Function to generate an anonymous fibonacci function as the return value -- (not tail-recursive) ///////
|
|
|
|
fib_gen @FN [f]
|
|
{ n -> { (n<=0) ? { 0 }
|
|
{ (n<=2) ? {1} { (@f n-1) + (@f n-2) } }
|
|
}
|
|
};
|
|
|
|
|
|
/////// loops to test the above functions ///////
|
|
|
|
@VAR factorial = @y fact_gen;
|
|
@VAR fibonacci = @y fib_gen;
|
|
|
|
@LOOP init:{@VAR i = -1} while:(i <= 20) next:{i++}
|
|
{ @SAY i "factorial =" (@factorial i) };
|
|
|
|
@LOOP init:{ i = -1} while:(i <= 16) next:{i++}
|
|
{ @SAY "fibonacci<" i "> =" (@fibonacci i) };
|