RosettaCodeData/Task/Y-combinator/M2000-Interpreter/y-combinator-2.m2000
2023-07-01 13:44:08 -04:00

34 lines
821 B
Text

Module Checkit {
Rem {
all lambda arguments passed by value in this example
There is no recursion in these lambdas
Y combinator make argument f as closure, as a copy of f
m(m, argument) pass as first argument a copy of m
so never a function, here, call itself, only call a copy who get it as argument before the call.
}
Y=lambda (f)-> {
=lambda f (x)->f(f,x)
}
fac_step=lambda (m, n)-> {
if n<2 then =1 else =n*m(m, n-1)
}
fac=Y(fac_step)
fib_step=lambda (m, n)-> {
if n<=1 then =n else =m(m, n-1)+m(m, n-2)
}
fib=Y(fib_step)
For i=1 to 10 {
Print fib(i), fac(i)
}
}
Checkit
Module CheckRecursion {
fac=lambda (n) -> {
if n<2 then =1 else =n*Lambda(n-1)
}
fib=lambda (n) -> {
if n<=1 then =n else =lambda(n-1)+lambda(n-2)
}
For i=1 to 10:Print fib(i), fac(i):Next
}
CheckRecursion