DEV Community

Cover image for Infix, prefix, postfix. Is it Matter?
Natserract
Natserract

Posted on

Infix, prefix, postfix. Is it Matter?

When learning a programming language, of course there are some mandatory things that should not be missed, such as understanding syntax, data types, operators, etc. So in this article, I will discuss about operators.

Operator is actually just a symbol used to perform certain mathematical or logical operations. There are many types of operators, for example: (+), (*), (++). The fact that we will not be able to escape from mathematics in life, especially in mathematical programming language that is functional programming. As one mathematician blogger say:

"After my involving myself in the subject, one thing that stands out is the relatively low distance between thought expressed in my ordinary day-to-day mathematical discourse, and thought expressed in Haskell code."

Most of the time we write mathematical expressions like this:

> 2 + 3
Enter fullscreen mode Exit fullscreen mode

This is a common method used both in programming languages ​​and in schools. If you look at the code above, the position of the operator (+) is in the middle operand, this is in accordance with the principle The main point of fp is that the value of an expression depends only on the value of its sub-expression.

Infix

Infix is ​​basically just a method used to perform an operation, where the operator position is placed between the operands, for example like this:

λ> 3 + 5 * 2
Enter fullscreen mode Exit fullscreen mode

That's operation is actually interpreted as "First multiply 5 * 2, then add the result by 3, then the final result is 13". Here some questions begin to arise, why the first operation evaluated is `() first, not (+)? Shouldn't the result be 3+5=8*2=16`??*

Ok, the answer is because all these operators have their own priority, in this table 9 is the highest. I found this out while reading fixity declarations on haskell.

+--------+----------------------+-----------------------+-------------------+
| Prec-  |   Left associative   |    Non-associative    | Right associative |
| edence |      operators       |       operators       |    operators      |
+--------+----------------------+-----------------------+-------------------+
| 9      | !!                   |                       | .                 |
| 8      |                      |                       | ^, ^^, **         |
| 7      | *, /, `div`,         |                       |                   |
|        | `mod`, `rem`, `quot` |                       |                   |
| 6      | +, -                 |                       |                   |
| 5      |                      |                       | :, ++             |
| 4      |                      | ==, /=, <, <=, >, >=, |                   |
|        |                      | `elem`, `notElem`     |                   |
| 3      |                      |                       | &&                |
| 2      |                      |                       | ||                |
| 1      | >>, >>=              |                       |                   |
| 0      |                      |                       | $, $!, `seq`      |
+--------+----------------------+-----------------------+-------------------+
Enter fullscreen mode Exit fullscreen mode

The table above relates to the order in which operations will be evaluated first.

After seeing how the priority/precedence/precedence is for each of the operators above, this time the case is slightly different, namely by adding brackets ( )

λ> (3 + 5) * 2
Enter fullscreen mode Exit fullscreen mode

That's operation mean's "first add 3 and 5, then multiply by 2, then the final result is 16. The question arises again, instead of `() having priority 7 > 6 (+)`??*

Ok, this is because infix notation has a rule that if there is a parenthesis ( ) then the highest priority is ( ) which is worth 13. So the result is 16.

Prefix (aka Polish)

Prefix otherwise known as "Polish notation", where the position of the operator is written before the operand. The expression is like this /* A + B C D. Take a look at the code below:

> (+) 3 5 * 2
Enter fullscreen mode Exit fullscreen mode

In the prefix operation, evaluation is carried out from left to right, based on the two values ​​closest to the right. If you refer to the code above, it means "first add 3 and 5, then multiply by 2, then the final result is 16". The first evaluation occurs at (3 + 5) because these two numbers are close to each other, just multiply by 2."

I give another case, an example of an expression like this + * 2 10 / 14 2. Evaluation happens "First multiply 2 and 10, divide 14 by 2, last 20 + 7 = 27".

By the way the prefix notation doesn't support brackets ( ), so it won't have any effect. Haskell has no prefix operators, with the exception of minus (-), which is both infix and prefix.

Postfix (aka !Polish)

Postfix is ​​called "reverse polish notation", where the operator is at the end of the operand. This notation is used in several old programming languages ​​such as Forth and Postscript . Take a look at the code below:

λ> (!) :: Bool -> Bool
λ> (!) = not
λ> (True !)
Enter fullscreen mode Exit fullscreen mode

In haskell itself to support postfix operators, yo've to use syntax extensions on GHC, so you don't get scolded by the compiler.

Yes, that's 3 kinds of notation used in writing expressions. Although prefer to infix syntax because it is easier to read, each has its own purpose and purpose. This article is important for you, especially those who are learning functional programming, where mathematics happens everywhere.

But most importantly, I hope this article is useful, Thanks!

Discussion (0)