Question: plz do this in lambda calculus Consult the definitions of if, true, false, and mult from the previous homework. We can combine Church numerals and

plz do this in lambda calculus
Consult the definitions of if, true, false, and mult from the previous homework. We can combine Church numerals and Church booleans in the iszero function, which will return true when its argument is Z and false for any other Church numeral: iszero = in.n (Ax.false) true We might therefore like to define a factorial function fact recursively like this: fact = In.if (iszero n) 1 (mult n (fact (pred n))) In prose: if n is zero, its factorial is 1. Otherwise, its factorial is n times the factorial of n - 1. This algorithm should be familiar to you. However, the above definition is not a valid expression in lambda calculus, because if we substitute each occurrence of the name fact with its definition, we will get an infinite expression: fact = In.if (iszero n) 1 (mult n (fact; (pred n))) = In.if (iszero n) 1 (mult n (An.if (iszero n) 1 (mult n (fact (pred n))) (pred n))) = An.if (iszero n) 1 (mult n (An.if (iszero n) 1 (mult n (An.if (iszero n) 1 (mult n (fact (pred n))) (pred n))) (pred n))) Is it possible to define recursion within lambda calculus, without changing its rules? Yes! We use this function, the so-called Y combinator: Y = \f.c.f (x )) (x.f (2x)) Y has the interesting property that, given a function, it will invoke that function with itself as its own parameter. This allows us to write functions that refer to themselves. Let's rewrite fact; using Y into facto: fact2 = Y (1f.An.if (iszero n) 1 (mult n (f (pred n)))) Note that the definition of fact2 does not refer to itself by name. Within the above definition, the function f refers to the expression in the parentheses, i.e. the parameter of Y. 3. Use the Y combinator to write a function sum that will calculate the sum of numbers 0+1+2+ ... +n. Use only Y, f, n, if, iszero, Z, plus, pred, and parentheses. Complete the template: sum = Y(f.An..---------) Consult the definitions of if, true, false, and mult from the previous homework. We can combine Church numerals and Church booleans in the iszero function, which will return true when its argument is Z and false for any other Church numeral: iszero = in.n (Ax.false) true We might therefore like to define a factorial function fact recursively like this: fact = In.if (iszero n) 1 (mult n (fact (pred n))) In prose: if n is zero, its factorial is 1. Otherwise, its factorial is n times the factorial of n - 1. This algorithm should be familiar to you. However, the above definition is not a valid expression in lambda calculus, because if we substitute each occurrence of the name fact with its definition, we will get an infinite expression: fact = In.if (iszero n) 1 (mult n (fact; (pred n))) = In.if (iszero n) 1 (mult n (An.if (iszero n) 1 (mult n (fact (pred n))) (pred n))) = An.if (iszero n) 1 (mult n (An.if (iszero n) 1 (mult n (An.if (iszero n) 1 (mult n (fact (pred n))) (pred n))) (pred n))) Is it possible to define recursion within lambda calculus, without changing its rules? Yes! We use this function, the so-called Y combinator: Y = \f.c.f (x )) (x.f (2x)) Y has the interesting property that, given a function, it will invoke that function with itself as its own parameter. This allows us to write functions that refer to themselves. Let's rewrite fact; using Y into facto: fact2 = Y (1f.An.if (iszero n) 1 (mult n (f (pred n)))) Note that the definition of fact2 does not refer to itself by name. Within the above definition, the function f refers to the expression in the parentheses, i.e. the parameter of Y. 3. Use the Y combinator to write a function sum that will calculate the sum of numbers 0+1+2+ ... +n. Use only Y, f, n, if, iszero, Z, plus, pred, and parentheses. Complete the template: sum = Y(f.An..---------)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
