4

In the example calculator:

REBOL [title: "Calculator"]
do %r3-gui.r3
stylize [
    btn: button [
        facets: [init-size: 50x50]
        actors: [on-action:[set-face f join get-face f get-face face]]
    ]
]
view [
    hgroup [
        f: field return
        btn "1"  btn "2"  btn "3"  btn " + "  return
        btn "4"  btn "5"  btn "6"  btn " - "  return
        btn "7"  btn "8"  btn "9"  btn " * "  return
        btn "0"  btn "."  btn " / "   btn "=" on-action [
            attempt [set-face f form do get-face f]
        ]
    ]
]

...the resulting program doesn't (as Rebol traditionally doesn't) evaluate mathematical expressions with * having a higher precedence than +. E.g. 2 + 3 * 4 gives 20 instead of 14.

I thought I had read somewhere that Rebol3 contained a new function that would evaluate math expressions more the way folks are used from nearly every other context. Is that true? If so, can the above code be made to use it without a significant amount of change?

draegtun
  • 22,441
  • 5
  • 48
  • 71
Kev
  • 15,899
  • 15
  • 79
  • 112
  • 2
    Adding `expr` to Rebol 3 was posted as a *wish* earlier this year - http://curecode.org/rebol3/ticket.rsp?id=2120 – draegtun Oct 22 '14 at 20:25

2 Answers2

4

I'm not sure there's a specific function in Rebol 3 that evaluates operators according to formal precedence (I'd be pleased to be corrected), though there are attempts in the wild to implement such a function. If you were to locate such a function, you can just change the evaluator from do to do-expression (where do-expression is said function) in the on-action block of your "=" button.

rgchris
  • 3,698
  • 19
  • 19
1

Who said that "the normal order" to evaluate should be the best? Some 'wiseguy' once came up with that because he could not handle being wrong unfortunately he was stronger than the others and threatened to hammer their heads in if they wouldnot do things like he had done, so we are taught to this day that multiplication precedes over addition despite of the order they were put in. Rebol rebels! ;)

iArnold
  • 333
  • 2
  • 10
  • Think about it: almost every kid makes this 'mistake' so maybe the rule is just arbitrary and counterintuitive. And as there are parenthesis to specify the order in case it is not normal left to right, maybe the rule should be abandoned for it is a bad rule. – iArnold Oct 22 '14 at 19:43
  • Fair enough argument, perhaps (I'm going to read http://stackoverflow.com/questions/17154700/why-do-languages-have-operator-precedence?rq=1 next, just saw over at the right...) but I never claimed it's the best. I just wondered how to make it what I'm now used to, or others may prefer. – Kev Oct 23 '14 at 09:09
  • c.f. http://stackoverflow.com/users/288875/andre-holzner about writing polynomials... – Kev Oct 23 '14 at 09:15