7

And what do you think about operator precedence? Would be harder programming in a language where the operations are executed in sequential order?

Ex.:

2 + 3 * 4 == 20

2 + (3 * 4) == 14

OK, the Lisp family doesn't have precedence by definition. Let's gonna talk about procedural and object-oriented languages using this "feature".

6
  • 1
    It would be a strange language indeed that doesn't have operator precedence that isn't generally the same as found in classical mathematics.
    – greyfade
    Commented Sep 14, 2010 at 2:25
  • This doesn't seem off topic to me. Is there a reason you decided to close your own question?
    – Macneil
    Commented Nov 25, 2010 at 5:24
  • @Macneil: It's objective and can be ask on Stackoverflow.
    – Maniero
    Commented Nov 25, 2010 at 9:02
  • 2
    This is a poll type question and I don't see the value in it. It's like asking "which languages don't support parameterized types" Commented Feb 25, 2013 at 23:25
  • Swift doesn’t have operator precedence in the language, but defined in the standard library or defined by the user. The language supports that operators have precedence, associativity but does not which one.
    – gnasher729
    Commented Feb 22, 2018 at 9:05

5 Answers 5

20

Smalltalk.

Everything's done with message sending, so 1 + 2 * 3 means "send * with parameter 3 to the object returned by sending the message + with parameter 2 to the object 1".

That throws people (it threw me) because of how we usually write maths, but since I can never remember C's operator precedence I cope in the same manner in both languages - I use ()s to group terms: 1 + (2 * 3).

12

Forth

It's (almost) all RPN notation, so no precedence rules needed. I'd wager most languages using postfix or prefix notation (PostScript, Lisp...) would work the same.

0
9

LISP-type languages don't need precedence because expressions are fully parenthesized. There is no need for precedence to evaluate

(sqrt (expt (- x1 x2) 2)
      (expt (- y1 y2) 2))

I know J, and I believe it's close relative K (along with their parent language, APL, as noted by @Jerry Coffin), evaluate everything right to left with no precedence.

3
  • Neither prefix notation (as in Lisp languages) nor postfix notation (HP calculators) need operator precedence. It's only infix that needs parentheses. Commented Sep 15, 2010 at 6:15
  • 6
    Prefix notation can do away with parentheses only if all operations have fixed arity. In the LISP family, the arithmetic operators can take more than two arguments (or less): (+) -> 0, (* 1 2 3 4) -> 24, etc. Commented Sep 15, 2010 at 14:50
  • Ah, you got me there! Quite right; I assumed "operator" meant "binary operator" Commented Sep 15, 2010 at 15:30
7

APL has no precedence. If memory serves, everything is grouped right to left.

Oddly, at least in an official sense, neither C nor C++ has operator precedence. The standard isn't written that way, although (of course) it's mostly a different way of saying the same thing as having precedence. OTOH, it is only mostly the same thing -- ultimately, there's no way to write a precedence table for C or C++ and get everything quite right. There are a few things that just won't quite fit.

3
  • 11
    C and C++ both have precedence. It's even a part of the formal grammar. I believe what you're talking about is the order of the evaluations of subexpressions. For example f() + g() * h() in C/C++ can call any of f, g, or h first, but it always computes (the equivalent of) the * happening before the +.
    – Macneil
    Commented Oct 29, 2010 at 1:02
  • 2
    APL is parsed right-to-left, but evaluated left-to-right.
    – Ven
    Commented Feb 21, 2018 at 13:45
  • @Ven - yes APL for the win!
    – davidbak
    Commented Feb 18, 2021 at 17:09
7

Prolog.

Well, kind of. There are standard operators, with standard precedence... but you can trivially define operators with arbitrary predence, because 1 + 2 is really the goal +(1, 2).

You may define infix (1 + 2), prefix (++X) and postfix (X++) operators, with arbitrary associativity (so left, right or both).

Not the answer you're looking for? Browse other questions tagged or ask your own question.