reset password
Author Message
rabbott
Posts: 1649
Posted 13:24 Oct 24, 2016 |

Haskell like most mathematical and programming notations allows you to leave out parentheses when using operators. For example

> 4 - 3 - 1
0

The implicit parenthesizing is 

> (4 - 3) - 1
0

Not

> 4 - (3 - 1)
2

It's important for you to know how arguments are combined by operators. So I may ask you about that.  For example, if you write

f . g . h x

what are the implicit parentheses? In this example, what must be the case for this, as written, to be syntactically valid?

rabbott
Posts: 1649
Posted 21:24 Oct 24, 2016 |

Here is a table of precedence and associativity. Function application may be considered precedence 10 and left associative: f g x is understood by default to mean: (f g) x. Usually that's not what you want. If you want f (g x) you have to include the explicit parentheses.

Note that (.) is right associative. So f . g . h means f . (g . h). Does that matter? Not really.  

f . (g . h) is the same as (f . g) . h. It's similar to  3 + (4 + 5) == (3 + 4) + 5.

 

+--------+----------------------+-----------------------+-------------------+
| 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`      |
+--------+----------------------+-----------------------+-------------------+
Last edited by rabbott at 21:25 Oct 24, 2016.