## Haskell’s prefix and infix notations

Like any other language out there, Haskell has possibility for basic arithmetic operations – addition, subtraction, multiplication, division. As I said in my prelude, there are languages in which these operations are represented by special operators and languages in which they are well respected citizens and have function status. Haskell is a gentleman and treats arithmetic operators as functions. This may seem weird at first if all of your experience is with main-stream languages like any of the C family of languages. In Lisp, having, let’s say `+`

as a function, was easy (although it looked a little bit awkward at first) because its place wasn’t between the two arguments, but rather in front of them:

```
;; Lisp code
;; evaluates to 3
(+ 1 2)
```

Haskell employs a clever concept in order to have basic arithmetic operators as first class citizens (i.e. functions), while still allowing us to write code in a traditional manner, with the operator placed in between the operands:

```
Prelude> 1 + 2
3
```

The cleverness I was talking about is that Haskell offers the possibility to call functions in two different notations:

- infix notation, the function is placed in between the arguments
- prefix or polish notation, what we’re used to with functions from main-stream languages

Because of this possibility we can write our additions to use polish notation too, almost as in Lisp:

```
Prelude> (+) 1 2
3
```

As you may have noticed, in this case, the function must be enclosed in parenthesis, for the convenience of the compiler I believe, but this is an exception from the rule. The normal rule states that functions used with the infix notation must be enclosed between two backtick characters.

```
Prelude> let add a b = a + b
Prelude> add 1 2
3
Prelude> 1 `add` 2
3
```

Under this new light, it makes perfect sense for arithmetic functions to not obey the standard rule for invoking functions using infix notation. Having to write `1 `+` 2`

would be really awkward. Much more awkward than Lisp’s solution in my opinion.

## Special rules when using negative numbers

Now, there’s one more thing to mention about Haskell’s arithmetic implementation. There is an issue with using negative numbers in compound calculations. Normally, in Haskell, a negative number is written as in any other language we know about. For example: `-1`

.

The minus operator is Haskell’s only unary arithmetic operator (or not?), i.e. it takes a single argument. Further more, it has the same level of precedence as the + function. Because of this precedence level, we can’t write:

```
Prelude> 1 + -1
<interactive>:1:0:
precedence parsing error
cannot mix `(+)' [infixl 6] and prefix `-' [infixl 6] in the same infix expression
```

Having both an addition and a subtraction in the above code, Haskell does not know which of them to apply first. So we have to enclose the negative number in a couple of parenthesis:

```
Prelude> 1 + (-1)
0
```

That was all for today’s infix and prefix/polish notation used for calling Haskell functions.

### References

- Real World Haskell. Chapter 1 and Chapter 2
- Learn You a Haskell for Great Good. Ready, set, go! and Baby’s first functions

P.S. While writing this post I realized that the statement “The minus operator is Haskell’s only unary arithmetic operator”, which appears in Real World Haskell may be a little wrong. I’m going to write about this in a future post.

## The minus operator in Haskell « Learning Haskell 11:12 pm

onJuly 23, 2009 Permalink |[…] minus operator in Haskell In a previous post, concerning the two ways Haskell does function application, I was using as examples arithmetic operations. Among other things I said (I actually cited from […]

## ray 11:25 pm

onJuly 29, 2009 Permalink |(+) being infix isn’t an exception to the rule – the rule is that a function whose name comprises “symbols” is an infix operator, and its name has to be surrounded in parentheses for the function to be used prefix, just like a regular function’s name has to be surrounded in backticks for it to be used infix. Unary minus is a special case, though.