4
$\begingroup$

In Prefix notation, the operator comes before the operands (e.g., +a b).
In Postfix notation, the operator comes after the operands (e.g., a b+).
In Infix notation, the operator comes in between the operands (e.g., a+b).

Most practical languages use infix notation for binary operators, but some use prefix or postfix.

So, what are the advantages and disadvantages of each type of notation?

$\endgroup$
5
  • 2
    $\begingroup$ There's also mixfix! It subsumes prefix, postfix, and infix, for better or for worse. $\endgroup$ May 17, 2023 at 17:02
  • 2
    $\begingroup$ TIL Lisp is an esolang. $\endgroup$
    – Adám
    May 17, 2023 at 17:10
  • 1
    $\begingroup$ Most practical languages use prefix too (+, -, ~, !). Many use postfix (?, !!) $\endgroup$
    – user
    May 17, 2023 at 17:17
  • 1
    $\begingroup$ @user I meant for binary operators only $\endgroup$
    – The Thonnu
    May 17, 2023 at 17:17
  • $\begingroup$ @Adám And on the postfix side, there's Forth and Factor $\endgroup$
    – user
    May 17, 2023 at 17:18

2 Answers 2

6
$\begingroup$

They really are just how people think about applying operators.

Infix is common arithmetic notation. Everyone learns a + b in school.

Prefix is really a function call. + a b is just like foo a b with no special parsing.

Postfix is a stack operation. a b + means push, push, and then pop/apply.

Ultimately users are applying an operator (a function), so the notation reflects how users think about it.

$\endgroup$
5
$\begingroup$

Infix notation is primarily borrowed from math. If you consider function calls as a sort of operator, you'll see that it is (most often) a prefix one. As for their pros and cons, the following come to mind:

  • Infix is, as said above, familiar to mathematicians, which has influenced general language design significantly in various areas.
  • Prefix and postfix notations can be parsed unambiguously without operator precedence. (The first one by a recursive parser, the second -- by a stack-based one).
  • Prefix notation (arguably) allows the programmer to grasp the code more easily while reading it left-to-right. Essentially, it allows to focus on the data knowing in advance what operators it is fed to. It bears similarity to the natural languages' grammatic forms, in fact: "Add two and the difference between five and four" ~ + 2 - 5 4. On the other hand, since this approach technically requires no delimiters/parentheses, it is easy to get lost in context, whereas with infix notation you'll usually be able to start from anywhere in the middle by looking for the nearest parentheses. Regardless, postfix is probably the worst approach in this sense: you need to memorize the stack before knowing the meaning of any specific item. Easy to parse for a machine, but hard to grasp for a human.
  • Infix notation doesn't scale well for higher -arities. The only way I know of is adding new operators to work in a group (for example, that's what Python's inline a if b else c does). That's why even languages that stick to infix for arithmetics tend to prefer either prefix or postfix for general functions
$\endgroup$
1
  • $\begingroup$ An example of a language that decided to use infix for everything is Smalltalk, and this concept was continued by Objective-C. $\endgroup$
    – Bbrk24
    May 17, 2023 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .