Prefix, Infix and Postfix

One of the odd things about Scheme compared with the C-style languages is it’s use of prefix notation for operators. In prefix notation you put the operator first followed by the things it acts on and enclose the whole lot in brackets. So for example if you want to write 3+4, in Scheme you say:

(+ 3 4)

In normal math when you write 3×4+2, you have to know some rules about multiplication and addition. First you multiply 3 by 4, then add 2. So you evaluate multiplication operators before addition operators. Multiplication has a higher precedence than addition.

The advantage of the prefix notation is that no operator precedence is required; the bracketing shows what the operator acts on. So in Scheme if you want 3×4+2, you say

(+ (* 3 4) 2)

And if you want 3x(4+2), you write:

(* 3 (+ 4 2))

So in Scheme you don’t need operator precedence, but you do need lots of brackets.

The reason Scheme makes this choice is it simplifies the job of the language interpreter. It doesn’t have to understand operator precedence, only how brackets work. I think though it goes a bit further than just saving the interpreter writer a bit of work though. One of Schemes key features is a common notation for both data and program representation, which means a program can be represented as data and data can be interpreted as a program. That’s quite a powerful feature, and I think the simple syntax of Scheme is part of what makes it possible.

Postfix notation

hp11c2-smallThere is another possible way of writing operators, with the postfix, or “reverse Polish” notation. In postfix notation, the operator comes after it’s operands. It has the advantage that you don’t need operator precedence and you don’t need brackets either.

Some computer languages, such as the printer language PostScript and the general purpose language Forth use postfix notation. But the most common use of postfix is probably in old style Hewlett-Packard calculators. I remember one of the old timers at my first job talking longingly about these machines, and the efficiency of postfix.

In postfix if you want to write 3+4, you write

3 4 +

And if you want 3×4+2 you write

3 4 * 2 +

Infix notation

Infix notation is the normal style of writing math. It requires brackets and operator precedence, and so is the most complex of the three forms. It does however have the massive advantage of familiarity, and is probably the most human readable of the three forms.

One unusual feature of the infix notation used in the C-style languages is the operator precedence of the bitwise operators & and | is incorrect. Dennis Ritchie explains the historical accident which led to the unusual operator precedence as follows:


The precedence of & and | were as they are now.




Personally I find the precedence of the logical operators and (&&) and or (||) surprising as well. For example in the expression:

a && b == c && d

I would expect (a && b) == (c && d), but in fact what you get is a && (b==c) && d. I guess you learn fairly early on is to use brackets where there could be any ambiguity.

15 thoughts on “Prefix, Infix and Postfix

  1. Bonjour, je suis spécialiste de l’activité physique adaptée (j’ai suivi une partie
    de ma formation à Québec en kinésiologie) et travaille en Suisse à l’Hôpital
    du Jura dans le cadre de la consultation d’obésité.

  2. If you wish for to improve your familiarity simply keep

  3. If we can look at the things in life we regret
    having done, maybe we can understand that other people are entitled to make
    mistakes, even when those mistakes hurt us. It is through the facial appearance, the quality of the clothings worn, and the luminosity
    and colours of the aura of the soul that the degree of
    evolution of the soul may be determined. 1- Irene Maxine Pepperberg,
    The Alex Studies, Harvard University Press, England,
    1999, pp. To come up with great ideas to solve
    a problem we usually use a method called brainstorming.
    been perceived objectively by anyone.

    to find out more and more.

  5. The hack software lets ?ou have unlimited money additions,
    generate gems ?nd speed up your bosts on t??s recreation. ?ecause thhe instrument ?s designed ?n such a approach that yo?
    ccan not detect ?t, youu will neve? know who’s utilizing ?t and w?o just isn’t.
    The data t??t you’r? using t?e hack software ?s rarey detected ?ecause ?t features ? un-detection proxy.

    Heere ?s m? web blog free racing rivals hack

  6. The bartenders, amazed by this feat of drinking turns to the man and goes ‘Desperate Housewives dvd boxset’s a big effort.
    You’re allowed to celebrate a job promotion, a marriage, or the birth
    of a child, but it’s supposed to be done with class and style.


  8. Prefix notation does not require the use of parentheses. For example `+ * 3 4 2 ` is a perfectly valid prefix expression which translates to `3 * 4 + 2` in infix.

    Parentheses are enforced by LISP and its derivatives because LISP follows a Von Neumann style of programming (i.e. programs are data). This allows LISP programs to modify themselves.

    Hence your statement (`It has the advantage that you don’t need operator precedence and you don’t need brackets either.`) is incorrect. Both prefix and postfix don’t require parentheses as long as the functions are of fixed arity.

  9. good but explain it more simply and use diagram when you try to explain it because a person can easily understand if diagram is made

Leave a Reply
