Course map
Haskell // 03

Functions

Read

Defining and applying

A function is defined like a value with parameters in front of the =. Its type lists the parameter types and the result, separated by arrows.

double :: Int -> Int
double n = n * 2

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

main :: IO ()
main = print (add (double 3) 4)   -- 10

Applying a function is just writing the arguments after it, separated by spaces: add 1 2, not add(1, 2). Function application binds tighter than anything else, so double 3 + 4 means (double 3) + 4.

Int -> Int -> Int reads as a function of two Ints returning an Int. Secretly it is a function of one Int returning a function of one Int, which is why add 1 on its own is a perfectly good function. This is called currying.

01 / 05