How to resolve the algorithm Y combinator step by step in the Elixir programming language
How to resolve the algorithm Y combinator step by step in the Elixir 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 Elixir programming language
Source code in the elixir programming language
iex(1)> yc = fn f -> (fn x -> x.(x) end).(fn y -> f.(fn arg -> y.(y).(arg) end) end) end
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(2)> fac = fn f -> fn n -> if n < 2 do 1 else n * f.(n-1) end end end
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(3)> for i <- 0..9, do: yc.(fac).(i)
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
iex(4)> fib = fn f -> fn n -> if n == 0 do 0 else (if n == 1 do 1 else f.(n-1) + f.(n-2) end) end end end
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(5)> for i <- 0..9, do: yc.(fib).(i)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Maple programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the TXR programming language
You may also check:How to resolve the algorithm Collections step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the Common Lisp programming language