prefix notation question

C

Chad

I'm supposed to write a program that calculates arithmetic expressions
using prefix notation. The one example our professor gave us is

(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))

Which evaluated sucessively becomes..

(+ (- 6) (* 2 3 4) (/ 3 1 -2)) //<----I don't see how he arrives at
this expression
(+ -6 24 -1.5)
16.5

I don't see how he gets (+ (- 6) (* 2 3 4) (/ 3 1 -2)) from (+ (- 6)
(* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))

Chad
 
J

Jussi Piitulainen

Chad said:
I did. He told me to come by during his office hours. I told him I
couldn't because of work. He hasn't responded back.

Chad

In (/ 3 1 -2), divided 3 by 1 and -2. Similarly in (- 2 3 1), subtract
3 and 1 from 2. This is the rule for two or more arguments to / and -.

With one argument, like in (- 6), the rule is different and the
notation means the appropriate inverse: additive inverse in case of -,
multiplicative in case of /.

The languages that use this notation tend to like -3/2 (an exact
rational computed from exact rationals) more than -1.5 (typically an
approximation).
 
C

Chad

In (/ 3 1 -2), divided 3 by 1 and -2. Similarly in (- 2 3 1), subtract
3 and 1 from 2. This is the rule for two or more arguments to / and -.

With one argument, like in (- 6), the rule is different and the
notation means the appropriate inverse: additive inverse in case of -,
multiplicative in case of /.

The languages that use this notation tend to like -3/2 (an exact
rational computed from exact rationals) more than -1.5 (typically an
approximation).

But what happened to the (*3) that was in the original arithemtic
expression?
 
C

Chad

But what happened to the (*3) that was in the original arithemtic
expression?

I mean, what happened to (+3)? This obviously has to be account for
somewhere.

Chad
 
J

Jussi Piitulainen

Chad said:
On Oct 17, 7:45 am, Chad wrote: ....

Never mind. I was one again experiencing a brain lapse.

:)

You _can_ add one number together.

Even no numbers! (+) would be 0, the additive identity element, and
(*) would be 1, the multiplicative identity element.

(However, (+3) looks jarring for the users of those other languages.
Make it (+ 3), with the space between the operator and operand.)
 
C

Chad

:)

You _can_ add one number together.

Even no numbers! (+) would be 0, the additive identity element, and
(*) would be 1, the multiplicative identity element.

(However, (+3) looks jarring for the users of those other languages.
Make it (+ 3), with the space between the operator and operand.)


And just for the record, the expression I had asked about was an
example derivation that our program was supposed to do. So in other
words, I just wanted to know how he arrived at the expression so that
I can write the corresponding code.

Chad
 
R

Roedy Green

(* 2 3 4)

this means multiply these three numbers together = 24 and that is the
value of that expression.

Years ago I wrote a similar parser in PL/I. I took great advantage of
recursive calls. I did not need to create parse tree, it was implicit
in the stack tree of methods, which had just called which.

The more practical way to solve it (probably not permitted at your
level) is to use a parser generator to create you a computer program
to parse expressions and create parse tree, which you can then
interpret to produce the value.

See http://mindprod.com/jgloss/parser.html
http://mindprod.com/jgloss/javacc.html

--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
R

Roedy Green

And just for the record, the expression I had asked about was an
example derivation that our program was supposed to do. So in other
words, I just wanted to know how he arrived at the expression so that
I can write the corresponding code.

One way to tackle such a problem is to enumerate several plausible
interpretations for each of the strange bits of notation. Then turn
the crank and find out which one in the real interpretation.
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top