Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write answer only for A,C & D in Scheme code. Don\'t use loops/assignment

ID: 3887603 • Letter: P

Question

Please write answer only for A,C & D in Scheme code. Don't use loops/assignments. And please don't copy answers from elsewhere.

Problem 2 [20pt] In the Church encoding of natural numbers, we have used the n-fold composition of f, written f"'. Intuitively, f" means applying function f for n times a) (6pt) Implement a function funPower, which takes a function f, an integer n and returns the function f. For example, (funPower sqrt 2) 16) should return 2. n. For example, (encode 2) should return 2 (i.e., the function f. Az, f (/1). returns n. For example, (decode (encode 2)) should return2. of ni x n2 (i.e., ni X n2). For example, (decode (MULT (encode 2) (encode 3))) should be 6. b) (4pt) Implement a function encode, which takes a natural number n and returns the church encoding of c) (4pt) Implement a function decode, which takes n (the church encoding of some natural number n) and d) (6pt) Implement a function MULT, which takes two church numbers n1, n2 and returns the church number

Explanation / Answer

a) (define (funPower f n)
(lambda(x) (if (<= n 1)
(f x)
(f (funPower f (- n 1))x))))

b) (define (encode n)
(funPower f 2)n)

c) (define (decode n)
(funPower finverse 2)n)

where finverse(f(n)=n

d) (define (MULT n1 n2)

(* n1 n2))