Question
create an expression calculator in racket. The calculator will prompt the user for an expression in prefix notation and calculate the result of the inputted
create an expression calculator in racket. The calculator will prompt the user for an expression in prefix notation and calculate the result of the inputted expression. A history will be kept so that previous results can be used in expressions. You are expected to handle possible errors in the user’s input.
When ran, the program should immediately prompt the user for an expression. It will evaluate that expression, printing an error message if an error is encountered. A simple generic message ”Invalid Expression” is alright. There should be a history, a simple list of values. Every time the program succeeds in evaluating the expression, the result will be added to the history and printed to the screen. Remember, this is a functional language. The history should be a parameter to your eval loop function. So, adding a value to history should involve consing it with the existing history. When the value is printed to screen, you should include its history id (or index). The id will always be the order it was added to history. So, the first value will be id 1, the second id 2, and so on. We can use something like the nth function we wrote in class for tictactoe, but using cons to construct the history will result the values being in reverse order of their id. That is, the most recent value will be first in the list. This can be remedied by reversing the list before giving it to nth.
An expression is a value, binary operation, or a unary operation.
+ – A binary operator that adds together the result of two expressions.
* – A binary operator that multiplies together the result of two expressions.
/ – A binary operator that divides the first expression by the second. Don’t forget that the user might try to divide by zero. This is an error.
- – A unary operator that negates the value of an expression. There is no subtraction. So, to subtract we can add to a negative number.
$n – n should be a number. A value specifying to use the history value corresponding to id n. any integer – A value representing a literal number
Expressions are in prefix notation and read from left to right. So, if I wanted to evaluate 2 ∗ $1 + $2, I would right +*2$1$2. White space can be used to divide tokens (like to numbers), but other wise is insignificant. In the previous example I could also write + *2 12. It is an error, if the expression is evaluated, but there are still text remaining. For instance, +1 2 2 is an error. The second 2 is extraneous and not part of the expression.
Include the following functions • string->list • list->string • string->number • char-numeric? • char-whitespace? • char=? • string=? • read-line • begin • display • displayln • apply
Use tail recursion and no mutable variables
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started