Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use Dr.Racket: Assume that an arithmetic expression is either a primitive expression - a number - or a compound expression - a list containing

Please useDr.Racket:

Assume that an arithmetic expression is either a primitive expression - a number - or a compound expression - a list containing an operator and any number of arithmetic expressions, in the prefix notation, just as in Scheme. Furthermore, let us use only the four operators +, -, *, and /.

Your task is to implement an interpreter for this arithmetic. In the following, object can be any Scheme object (possibly not an arithmetic expression), while expr is expected to be an arithmetic expression.

Implement the following procedures. Do not use the built in eval function because that is what your evaluate should do.

  1. The predicate (operator? object) returns #t if object is any of +, -, *, and /. Hint: look at the member function.
  2. The procedure (null-val operator) returns the null value corresponding to an operator (i.e., 0 for + and -, 1 for * and /). You might use the assoc function to implement this.
  3. The predicate (expression? expr) returns #t iff expr is a valid arithmetic expression (see note above detailing what makes a valid expression). Use the (built-in) predicate list? to test if expr is a list, the (built-in) length to find out the length of a list, and operator? and the (built-in) predicate number? to test operators and operands. The andmap function will help with this...by allowing you to appl the expression? function to each of the operands and anding together the result.
  4. The procedure (count-operators expr) returns the total count of operators in the expression (including those in nested compound expressions, if any). Similarly, (count-primitive-operands expr) returns the total count of primitive operands (i.e., numbers) in the expression.
  5. The procedure (evaluate expr) returns the arithmetic value of the expression (the result of actually applying the operators to the operands). If you have an operator named op, you can apply that to operands by using it's name followed by the operands (evaluated recursively). Alternatively, and more conveniently, you should be able to evaluate the operands by applying the function map to evaluate and the operand list, and then you can use the apply function to apply an operator to a list of evaluated operands.

Examples:

;; 1 + (2 / (3 * 5 + 1)) + (-4) (define expr (list + 1 (list / 2 (list + (list * 3 5) 1)) (list - 0 4))) expr ;Value: (+ 1 (/ 2 (+ (* 3 5) 1)) (- 0 4)) (operator? (car expr)) ;Value: #t (number? (cadr expr)) ;Value: #t (expression? expr) ;Value: #t (expression? (list 1 + 2)) ;Value: #f (expression? (list + 1)) ;Value: #f (count-operators expr) ;Value: 5 (count-primitive-operands expr) ;Value: 7 (evaluate expr) ;Value: -23/8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

1583473858, 978-1583473856

More Books

Students also viewed these Databases questions