Project代做 | Racket代做 – Racket Project and Macros

Project代做 | Racket代做 – 这是一个racket基础训练题目

racket project and Macros

The purpose of this assignment is to give you experience with writing a larger program in Racket, and also with using Racket macros. You can (and should) use side effects, with care, in Question 3. Dont use side effects for the answers to any of the other questions.

Points: 15 points Question 1, 4 points each Questions 2, 3 and 4.

You can use up to 3 late days for this assignment.

Turnin: Turn in two files: polynomials.rkt, with the functions and unit tests for Questions 1 and 2; and macros.rkt, with the functions, macros, and unit tests for Questions 3 and 4. You dont need to turn in sample output the unit tests are enough for those. As before, your program should be tastefully commented (i.e. put in a comment before each function definition saying what it does). Style counts!

1. Write and test a Racket functionpoly-multiplythat multiplies two polynomials in a symbolic variable and returns the result. The polynomials should be represented as lists of terms, where each term is in turn a list consisting of a coefficient and an exponent of the symbolic variable. The polynomials should be normalized: they should be sorted with the largest exponent first, there shouldnt be two terms with the same exponent, and that shouldnt be any terms with a coefficient of 0. You can assume that the exponents will be non-negative integers. The zero polynomial is represented as the empty list. The result returned bypoly-multiplyshould be normalized as well. For example:
``````(poly-multiply '((1 3) (1 2) (1 1) (1 0)) '((1 1) (-1 0)))
``````
``````should evaluate to'((1 4) (-1 0)).
In standard algebraic notation, this represents
x^4 1 = (x^3 +x^2 +x+ 1)(x1)
Here are some other polynomial pairs that you can turn into unit tests for your function:
( 3 x^4 +x+ 5) 0
0 x^2
(x^3 +x1) 5
( 10 x^2 + 100x+ 5)(x^999 x^7 +x+ 3)
3 x
x 3
``````
1. Define a functionpoly->codethat converts a polynomial in normalized form as in Question 1 into evaluable Racket code. It should take two parameters: the list representing the polynomial, and the symbolic variable. Here are some examples:
``````(poly->code '((1 3) (5 2) (7 1) (10 0)) 'x) =>
'(+ (expt x 3) (* 5 (expt x 2)) (* 7 x) 10)
``````
``````(poly->code '((1 1) (-10 0) 'x) => '(+ x -10)
``````

1

``````(poly->code '((1 1)) 'x) => 'x
``````
``````(poly->code '((10 0)) 'x) => 10
``````
``````(poly->code '((1 0)) 'x) => 1
``````
``````(poly->code '() 'x) => 0
``````
``````So in general, the result will be a sum of terms, but if there is just one term, just return that term. In
general a term is the coefficient times the variable raised to the exponent; but omit the coefficient if it
is 1 and the exponent isnt 0, omit the exponent if it is 1, and omit both the variable and the exponent
if the exponent is 0. Finally, return 0 for the zero polynomial.
Add appropriate unit tests for yourpoly->codefunction.
Since the result returned frompoly->codeis legal Racket code, we should be able to evaluate it.
Suppose we define two polynomialsp1andp2and a numberxin the interaction pane:
``````
``````(define p1 '((1 3) (1 2) (1 1) (1 0)))
(define p2 '((1 1) (-1 0)))
(define x 4)
``````
``````Then both of these expressions should evaluate to the same number, namely 255:
``````
``````(eval (poly->code (poly-multiply p1 p2) 'x))
(* (eval (poly->code p1 'x)) (eval (poly->code p2 'x)))
``````
``````(If you use these examples in the definition pane, for example in unit tests, youll need to use an