I know how to use stack to process an arithmetic expression string like `(1 + 2) * 3′. Is there a typical Scheme solution to this issue?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s a little hard to tell what question you’re asking. In Scheme (or Racket), you’d almost certainly write such an evaluator “directly”, like this:
… so the evaluator is going to be literally three lines long.
Note that in this definition, there’s no need to keep track of a stack explicitly (of course, you
can do the same thing in any other language–you’d use an explicit stack only if you wanted to write
the evaluator in the form of a loop, and didn’t want to separate the parsing step).
To parse the expression, you need… well, you probably want a parser. If you’re using Racket,
you could take a look at the calculator example that comes with Racket in
collects/parser-tools/examples/calck.rkt. It handles everything you describe. I could paste it all in here, but that’s probably overkill.