HomeworkInclude display statements and function calls as necessary to completely demonstrate that your code works for each of the following problems. Email your racket file to me when done (normally a .rkt extension).
Problem 1
Given the implementation of f1 as below, implement f2 and f3 such that:
f2 uses let instead of let*
f3 uses lambda instead of let*
(define (f1 a b)
(let* ((x (* 2 a))
(y (* 3 x)))
(* x y)))
(f1 4 5)
Ensure that f1 and f2 produce the same results as f1. Define no external variables.
Problem 2
Rewrite each of the lambdas using a let.
(define (bam a)
((lambda (x) (* x 5))
(+ a 20)))
(bam 3)
(define (wam a b c)
((lambda (w x y) (* w x y))
(+ a b) (* a b) (- a b)))
(wam 4 5 6)
(define (zam a b c)
((lambda (x)
((lambda (y) (expt y c))
(* x 3)))
(+ a b)))
(zam 5 6 7)
Remember to hit CTRL+i in drracket to correct indentation anytime things don’t seem to be working correctly, and then look for things that don’t line up properly.
Problem 3
Redefine wacky so that the procedure and all subproce- dures have the same behavior, but each has unique names (i.e. x1, x2, . . . , y1, y2, . . . ) for its local variables.
(define (wacky x y)
(define (tacky x z)
(define (snacky y z)
(+ x y z))
(snacky z x))
(tacky y x))
(wacky 3 4)
Do the reverse process of the previous problem. Rewrite f1, g and h so that no more than 3 variable names (a, b, c) are needed for the arguments.
(define (f1 a b)
(define (g x y) (* x y))
(define (h m n) (- m n))
(+ (g a b) (h b a)))
Do the same with f2.
(define (f2 a b)
(define (g x y) (+ a x y))
(define (h m n) (* b m n))
(+ (g a b) (h b a)))
(f2 3 4)
Rewrite f3 using only the function names foo and bar, and the variables x and y.
(define (f3 a)
(define (g1 b)
(define (h1 c) (expt c a))
(define (h2) (- a b))
(+ (h1 b) (h2)))
(+ a (g1 a)))
(f3 3)
Problem 4
A univariate polynomial of order n is given by the following equation.
Pn (x) = anxn + . . . + a2x2 + a1x + a0
Here, ai are the coefficients of the polynomial, and x is its unique variable. You might implement a procedure poly-3 for computing the polynomial of order 3 of x as follows.
(define (poly-3 x a0 a1 a2 a3)
(+ a0 (* a1 x) (* a2 x x) (* a3 x x x)))
In poly-3, the coefficients and the variable are bundled together as arguments; and you would have to specify the coefficients each time you want to compute the same polynomial with different values of x. Instead, implement the procedure make-poly-3 that generates a procedure that computes the polynomial for an arbitrary x.
(define (make-poly-3 a0 a1 a2 a3)
…)
(define my-poly-3
(make-poly-3 1 2 3 4))
(my-poly-3 2)
Next, write a function sum-poly-3-range which will sum up the results for calling my-poly-3 for the values in a range:
(define (sum-poly-3-range from to)
…)
(sum-poly-3-range 1 50)