Functions

Functions

When we define a new function, we can give it a type signature. For example double is a function takes an Integer and gives an Integer doubled as output.

double :: Integer -> Integer
double x = x * 2

Lambda Expression

There are also lambda expressions in Hamler, here is an example of how we rewrite double.

double' :: Integer -> Integer
double' = \x -> 2 * x

It becomes really handy when we need to make an anonymous function.

Currying

--Curry
--This is uncurried (+)
add :: (Integer, Integer) -> Integer
add (x, y) = x + y

--This is curried (+)
plus :: Integer -> Integer -> Integer
plus x y = x + y

Partial Application

-- plus :: Integer -> (Integer -> Integer) This is one of the example of higher order functions
>:t plus 2
plus 2:: Integer -> Integer
>let plusTwo = plus2
>plusTwo 3
5