How to resolve the algorithm Y combinator step by step in the Lua programming language
How to resolve the algorithm Y combinator step by step in the Lua programming language
Table of Contents
Problem Statement
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's state is used in the body of the function. The Y combinator is itself a stateless function that, when applied to another stateless function, returns a recursive version of the function. The Y combinator is the simplest of the class of such functions, called fixed-point combinators.
Define the stateless Y combinator and use it to compute factorials and Fibonacci numbers from other stateless functions or lambda expressions.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Y combinator step by step in the Lua programming language
Source code in the lua programming language
Y = function (f)
return function(...)
return (function(x) return x(x) end)(function(x) return f(function(y) return x(x)(y) end) end)(...)
end
end
almostfactorial = function(f) return function(n) return n > 0 and n * f(n-1) or 1 end end
almostfibs = function(f) return function(n) return n < 2 and n or f(n-1) + f(n-2) end end
factorial, fibs = Y(almostfactorial), Y(almostfibs)
print(factorial(7))
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Comal programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Nim programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the Racket programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Same fringe step by step in the Clojure programming language