# Functions

Chapter will be more complete soon. For now, check out the Functions cheatsheet of PureScript as functions work the same way in Haskell.

## Function composition​

The art of letting functions work together, so for example f1(f2(x)) is called function composition. There are two important signs you should know in Haskell.

In fact, composing functions like f1(f2(f3(x))) isn't a technical problem - yet is not that pretty. In Haskell, the $and . notation offer some syntactial sugar. ##$ (Dollar sign notation)​

double :: Int -> Intdouble x = x * 2triple :: Int -> Inttriple x = x * 3-- possible:double (triple 2)-- cleaner:double $triple 2 As you can see, the$ notation saves us the parentheses, and even provides for the closing one. Instead of having nested parentheses, your code might look more like this:

f1 $f2$ f3 $f4 69 ## . (Dot notation)​ The . operator helps us to tie the result of a function on the right side, to the input on the left side. Back to our example: -- before:double$ triple 2-- now:(double . triple) 2

To divide our almost final result by two:

(divideByTwo . double . triple) 2

I guess you now have a good feeling for both operators.

## Working with typeclasses as function parameters.​

Usually, we strictly define the types of values our function should take and return in the function declaration:

add :: Int -> Int -> Intadd x y = x + y

Yet, sometimes we want to use a broader spectrum of types that might be applied to our function. Coming back to the add-function, we must keep in mind that not just integer-values can be added. To make our function more general, we can instead of mentioning single types, use the whole class:

add2 :: Num x => x -> x -> xadd2 x y = x + y

Now we can even execute "add 2 2.5" successfully.