Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8897 > unrolled thread

prefix notation question

Started byChad <cdalten@gmail.com>
First post2011-10-16 20:15 -0700
Last post2011-10-18 19:36 -0700
Articles 12 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  prefix notation question Chad <cdalten@gmail.com> - 2011-10-16 20:15 -0700
    Re: prefix notation question Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-16 20:46 -0700
      Re: prefix notation question Chad <cdalten@gmail.com> - 2011-10-16 20:58 -0700
        Re: prefix notation question Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-16 23:38 -0700
        Re: prefix notation question Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-10-17 10:06 +0300
          Re: prefix notation question Chad <cdalten@gmail.com> - 2011-10-17 07:39 -0700
            Re: prefix notation question Chad <cdalten@gmail.com> - 2011-10-17 07:45 -0700
              Re: prefix notation question Chad <cdalten@gmail.com> - 2011-10-17 07:50 -0700
                Re: prefix notation question Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-10-17 18:10 +0300
                  Re: prefix notation question Chad <cdalten@gmail.com> - 2011-10-18 15:30 -0700
                    Re: prefix notation question Roedy Green <see_website@mindprod.com.invalid> - 2011-10-18 19:40 -0700
    Re: prefix notation question Roedy Green <see_website@mindprod.com.invalid> - 2011-10-18 19:36 -0700

#8897 — prefix notation question

FromChad <cdalten@gmail.com>
Date2011-10-16 20:15 -0700
Subjectprefix notation question
Message-ID<c865e53f-1e45-420b-aef3-cfb37216db89@13g2000prp.googlegroups.com>
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

[toc] | [next] | [standalone]


#8898

FromPeter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
Date2011-10-16 20:46 -0700
Message-ID<BZGdnRzdc-qLOAbTnZ2dnUVZ_vydnZ2d@posted.palinacquisition>
In reply to#8897
On 10/16/11 8:15 PM, Chad wrote:
> 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)))

Ask your professor.

[toc] | [prev] | [next] | [standalone]


#8899

FromChad <cdalten@gmail.com>
Date2011-10-16 20:58 -0700
Message-ID<0142e456-9e9a-4f71-adaa-5ea9cad35427@u9g2000vby.googlegroups.com>
In reply to#8898
On Oct 16, 8:46 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> On 10/16/11 8:15 PM, Chad wrote:
>
> > 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)))
>
> Ask your professor.

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

[toc] | [prev] | [next] | [standalone]


#8900

FromPeter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
Date2011-10-16 23:38 -0700
Message-ID<b5udnbZuWfzvUAbTnZ2dnUVZ_vmdnZ2d@posted.palinacquisition>
In reply to#8899
On 10/16/11 8:58 PM, Chad wrote:
>> [...]
>> Ask your professor.
>
> 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.

None of that does anything to make your question any more of a Java 
question, nor any more a question that should be answered by someone 
other than your professor.

Personally, I don't see what the difficulty is.  The main thing to 
understand is that your professor's use of prefix notation includes the 
idea that an operator can have variable arity, and he obviously has 
rules for how to deal with that (using the parentheses).

But this isn't the place for your homework assignments to be explained. 
  Your professor needs to be involved, so he can improve the clarity of 
the assignments and understand the level at which his students are 
operating.

It's his job to help you learn.  Insist that he do that job.

Pete

[toc] | [prev] | [next] | [standalone]


#8901

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2011-10-17 10:06 +0300
Message-ID<qotbotg5c7i.fsf@ruuvi.it.helsinki.fi>
In reply to#8899
Chad <cdalten@gmail.com> writes:

> On Oct 16, 8:46 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> > On 10/16/11 8:15 PM, Chad wrote:
> >
> > > 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)))
> >
> > Ask your professor.
> 
> 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).

[toc] | [prev] | [next] | [standalone]


#8913

FromChad <cdalten@gmail.com>
Date2011-10-17 07:39 -0700
Message-ID<7889cbdf-4b32-413f-bae0-daea3814e85a@f13g2000vbv.googlegroups.com>
In reply to#8901
On Oct 17, 12:06 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> Chad <cdal...@gmail.com> writes:
> > On Oct 16, 8:46 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> > > On 10/16/11 8:15 PM, Chad wrote:
>
> > > > 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)))
>
> > > Ask your professor.
>
> > 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).

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

[toc] | [prev] | [next] | [standalone]


#8914

FromChad <cdalten@gmail.com>
Date2011-10-17 07:45 -0700
Message-ID<ae7f17fd-8825-4c5c-a185-0abed998aea5@a12g2000vbz.googlegroups.com>
In reply to#8913
On Oct 17, 7:39 am, Chad <cdal...@gmail.com> wrote:
> On Oct 17, 12:06 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
> wrote:
>
>
>
>
>
> > Chad <cdal...@gmail.com> writes:
> > > On Oct 16, 8:46 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> > > > On 10/16/11 8:15 PM, Chad wrote:
>
> > > > > 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)))
>
> > > > Ask your professor.
>
> > > 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).
>
> 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

[toc] | [prev] | [next] | [standalone]


#8915

FromChad <cdalten@gmail.com>
Date2011-10-17 07:50 -0700
Message-ID<0135959f-5846-4869-9c5a-dd08998015e9@u13g2000vbx.googlegroups.com>
In reply to#8914
On Oct 17, 7:45 am, Chad <cdal...@gmail.com> wrote:
> On Oct 17, 7:39 am, Chad <cdal...@gmail.com> wrote:
>
>
>
>
>
> > On Oct 17, 12:06 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
> > wrote:
>
> > > Chad <cdal...@gmail.com> writes:
> > > > On Oct 16, 8:46 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> > > > > On 10/16/11 8:15 PM, Chad wrote:
>
> > > > > > 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)))
>
> > > > > Ask your professor.
>
> > > > 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).
>
> > 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.
>

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

Chad.

[toc] | [prev] | [next] | [standalone]


#8917

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2011-10-17 18:10 +0300
Message-ID<qotty77wt5o.fsf@ruuvi.it.helsinki.fi>
In reply to#8915
Chad writes:
> On Oct 17, 7:45 am, Chad wrote:
...
> > I mean, what happened to (+3)? This obviously has to be account
> > for somewhere.
> 
> 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.)

[toc] | [prev] | [next] | [standalone]


#8965

FromChad <cdalten@gmail.com>
Date2011-10-18 15:30 -0700
Message-ID<67b5c18e-b6f5-4004-87c3-b745cb2b1bf6@27g2000prq.googlegroups.com>
In reply to#8917
On Oct 17, 8:10 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> Chad writes:
> > On Oct 17, 7:45 am, Chad wrote:
> ...
> > > I mean, what happened to (+3)? This obviously has to be account
> > > for somewhere.
>
> > 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.)


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

[toc] | [prev] | [next] | [standalone]


#8970

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-10-18 19:40 -0700
Message-ID<d2es97ds9872cor9ko7pkf08peq22ed54k@4ax.com>
In reply to#8965
On Tue, 18 Oct 2011 15:30:28 -0700 (PDT), Chad <cdalten@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>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.

[toc] | [prev] | [next] | [standalone]


#8969

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-10-18 19:36 -0700
Message-ID<7kds97tus5c4ol8n38l21091q78a81u9qj@4ax.com>
In reply to#8897
On Sun, 16 Oct 2011 20:15:19 -0700 (PDT), Chad <cdalten@gmail.com>
wrote, quoted or indirectly quoted someone who said :

> (* 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web