operator precedence, just curious...
[1/7] from: tag-yer-it::mailandnews::com at: 5-Oct-2001 14:43
Hi guys,
Whats up with this? I'm just curious, and probably missing
something simple...
REBOL/Core 2.5.0.4.2 23-Mar-2001
Copyright 2000-2001 REBOL Technologies. All rights reserved.
REBOL >>> 6 * 7 - 3 / 4
== 9.75
Python 1.5.2 (#0, Dec 27 2000, 13:59:38) [GCC 2.95.2 20000220 (Debian GNU/Linux)] on
linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 6 * 7 - 3 / 4
42
--
signature sneezing: "achoo!"
-tom
[2/7] from: geza67:freestart:hu at: 5-Oct-2001 20:58
Hello Tom,
REBOL >>>> 6 * 7 - 3 / 4
> == 9.75
There is no such thing as operator precedence in REBOL. Expressions
are _always_ evaluated from left to right. If you would like to achieve the
usual result in the mathematical sense, use parentheses to group
subexpressions.
--
Best regards,
Geza mailto:[geza67--freestart--hu]
[3/7] from: greggirwin:starband at: 5-Oct-2001 13:38
Hi Tom,
<< Whats up with this? I'm just curious, and probably missing something
simple...>>
You have to use parens in REBOL to force operator precedence:
>> (6 * 7) - (3 / 4)
== 41.25
Or you could also do it this way if you're an RPN kinda' guy:
>> subtract multiply 6 7 divide 3 4
== 41.25
--Gregg
[4/7] from: ryanc:iesco-dms at: 5-Oct-2001 12:36
Hey Tom,
I actually like the REBOL way better myself, but I have never liked operator precedence
rules
either. My guess is that RT figured operator precedence interfered with the interpreters
design too much. Instead they compromised on the op type. Where most functions in rebol
take
thier arguments subsequently, op types take an argument from the previous word, as well
as the
following word. The result is a left to right evaluated equation, or as one might say
"no
precedence", though I am sure Joel has a more appropriate term for this. : )
I like it no precedence becuase it works exactly like how you would enter it into a calculator.
so the expression:
>> 6 * 7 - 3 / 4
is computed (((6 * 7) - 3) / 4) or just as if you typed "6 * 7 - 3 / 4" into a calculator.
If you still miss those operator precedence rules, somebody did write a dialect that
does it, I
am sure its easy to find.
--Ryan
Tom Foster wrote:
> Hi guys,
> Whats up with this? I'm just curious, and probably missing
<<quoted lines omitted: 14>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Ryan Cole
Programmer Analyst
www.iesco-dms.com
707-468-5400
[5/7] from: joel:neely:fedex at: 5-Oct-2001 17:59
Hi, all,
Gregg Irwin wrote:
> You have to use parens in REBOL to force operator precedence:
>
> >> (6 * 7) - (3 / 4)
> == 41.25
>
> Or you could also do it this way if you're an RPN kinda' guy:
>
> >> subtract multiply 6 7 divide 3 4
> == 41.25
>
Not that I'd ever be picky... ;-)
... but that's actually PN (Polish Notation, in which the operator
comes before its operands), not RPN (Reverse Polish Notation, in
which the operator follows its operands).
RPN is most commonly seen in FORTH (and certain H-P calculators),
where the above expression would have been written as
6 7 * 3 4 / -
(Elvis has backed out of the building!)
-jn-
--
The only thing scarier than a sysadmin with a screwdriver is a
programmer with the root password.
-- Matt Korth
joel^FIX^PUNCTUATION^dot^neely^at^fedex^dot^com
[6/7] from: g:santilli:tiscalinet:it at: 6-Oct-2001 15:21
Hello Ryan!
On 05-Ott-01, you wrote:
RC> If you still miss those operator precedence rules, somebody
RC> did write a dialect that does it, I am sure its easy to find.
It is not online, but maybe I should put it on my REB site?
>> eval [6 * 7 - 3 / 4]
== 41.25
To avoid the overhead of parsing when doing calculation in a loop
you can use the /translate refinement to translate from infix to
prefix notation.
>> eval/translate [6 * 7 - 3 / 4]
== [subtract multiply 6 7 divide 3 4]
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
REBOL [
Title: "Expression evaluator"
Author: "Gabriele Santilli <[giesse--writeme--com]>"
Comment: {
Evaluates expressions taking usual operator precedence
into account.
1. (...)
2. - [negation], ! [factorial]
3. ^ [power]
4. *, /
5. +, - [subtraction]
}
]
; A simple iterative implementation; returns 1 for negative
; numbers. FEEL FREE TO IMPROVE THIS!
factorial: func [n [integer!] /local res] [
if n < 2 [return 1]
res: 1
; should avoid doing the loop for i = 1...
repeat i n [res: res * i]
]
expression-parser: make object! [
slash: to-lit-word first [ / ]
expr-val:
expr-op: none
expression: [
term (expr-val: term-val)
any [
['+ (expr-op: 'add) | '- (expr-op: 'subtract)]
term (expr-val: compose [(expr-op) (expr-val) (term-val)])
]
]
term-val:
term-op: none
term: [
pow (term-val: power-val)
any [
['* (term-op: 'multiply) | slash (term-op: 'divide)]
pow (term-val: compose [(term-op) (term-val) (power-val)])
]
]
power-val: none
pow: [
unary (power-val: unary-val)
opt ['^ unary (power-val: compose [power (power-val) (unary-val)])]
]
unary-val:
pre-uop:
post-uop: none
unary: [
(post-uop: pre-uop: [])
opt ['- (pre-uop: 'negate)]
primary
opt ['! (post-uop: 'factorial)]
(unary-val: compose [(post-uop) (pre-uop) (prim-val)])
]
prim-val: none
; WARNING: uses recursion for parens.
primary: [
set prim-val [number! | word!]
| set prim-val paren! (prim-val: translate to-block :prim-val)
]
translate: func [expr [block!] /local res recursion] [
; to allow recursive calling, we need to preserve our state
recursion: reduce [
:expr-val :expr-op :term-val :term-op :power-val :unary-val
:pre-uop :post-uop :prim-val
]
res: if parse expr expression [expr-val]
set [
expr-val expr-op term-val term-op power-val unary-val
pre-uop post-uop prim-val
] recursion
res
]
set 'eval func [expr [block!] /translate] [
expr: self/translate expr
either translate [expr] [do expr]
]
]
[7/7] from: greggirwin:starband at: 6-Oct-2001 11:40
Hi Joel,
<< ... but that's actually PN (Polish Notation, in which the operator
comes before its operands), not RPN (Reverse Polish Notation, in
which the operator follows its operands). >>
DOH! Of course you are correct. My little HP20S doesn't work quite like the
old ones did (my Dad had an HP65, programmable, with the magnetic strip
reader) so I'm out of practice. :)
--Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted