Anonymous recursion: Difference between revisions
Content added Content deleted
(added Ol) |
|||
Line 1,361: | Line 1,361: | ||
<lang M2000 Interpreter> |
<lang M2000 Interpreter> |
||
Function fib(x) { |
|||
⚫ | |||
If x<0 then Error "argument outside of range" |
|||
If x<2 then =x : exit |
|||
⚫ | |||
=fib1(x) |
|||
} |
|||
Module CheckIt (&k()) { |
|||
Print k(12) |
|||
} |
|||
CheckIt &Fib() |
|||
Print fib(-2) ' error |
|||
</lang> |
|||
Using lambda function |
|||
<lang M2000 Interpreter> |
|||
fib=lambda -> { |
|||
fib1=lambda (x)->If(x>1->lambda(x-1)+lambda(x-2), x) |
|||
=lambda fib1 (x) -> { |
|||
If x<0 then Error "argument outside of range" |
|||
If x<2 then =x : exit |
|||
=fib1(x) |
|||
} |
|||
}() ' using () execute this lambda so fib get the returned lambda |
|||
Module CheckIt (&k()) { |
Module CheckIt (&k()) { |
||
Print k(12) |
Print k(12) |
||
} |
} |
||
CheckIt &Fib() |
CheckIt &Fib() |
||
Try { |
|||
Print fib(-2) |
|||
} |
|||
Print Error$ |
|||
Z=Fib |
|||
Print Z(12) |
|||
Dim a(10) |
|||
a(3)=Z |
|||
Print a(3)(12)=144 |
|||
Inventory Alfa = "key1":=Z |
|||
Print Alfa("key1")(12)=144 |
|||
</lang> |
</lang> |
||