Functions, application and layout
A definition
A function is a name on the left of = and an expression on the right, and the arguments sit between them:
add :: Int -> Int -> Int
add a b = a + b
The first line is the type, the second the definition. The type says "takes
an Int, then an Int, and
gives an Int"; the definition says what it gives. The
two are checked against each other, so a signature cannot drift away from
the code it describes.
There is no return and no braces: the expression on
the right is the result. Calling the function means replacing
add 2 3 with 2 + 3 and then
with 5, which is the whole evaluation model of the
language. A second definition can build on the first:
double :: Int -> Int
double x = add x x
Application is a space
To call a function, write its name and its arguments separated by spaces.
There are no parentheses around the argument list, and there are no commas:
add 2 3 applies add to
2 and 3.
Application binds tighter than any operator, so
add 2 3 * 2 is (add 2 3) * 2
and not add 2 (3 * 2). It is also left-associative:
f a b means (f a) b, a call
of f with a whose result is
called with b. That is not a detail of syntax; the
next lesson but one is about what it makes possible.
Parentheses are needed only where the argument is itself an expression that
should be computed first, as in double (add 1 1).
Without them, double add 1 1 passes the function
add as an argument and then two numbers, which is a
different program and a type error.
A file of functions
One file can hold as many definitions as it needs, and main can call them all:
module Main (main) where
add :: Int -> Int -> Int
add a b = a + b
double :: Int -> Int
double x = add x x
twice :: (a -> a) -> a -> a
twice f x = f (f x)
main :: IO ()
main = do
print (add 2 3)
print (double (add 1 1))
print (twice double 5)
print (7 `div` 2 :: Int)
print (div 7 2 :: Int)
ghc -Wall -O1 -o functions Functions.hs
./functions
[1 of 2] Compiling Main ( Functions.hs, Functions.o )
[2 of 2] Linking functions
5
4
20
3
3
Four of the five lines are the same arithmetic in different clothes, and the
third one is the interesting one: twice double 5 is
double (double 5), which is 20. The function
double was passed as an argument, not called:
functions are values like numbers, with types like any other value.
The signature of twice is the first one on the page
with a lowercase type variable. (a -> a) means
"a function from some type to the same type", and the two
as must match: this version works for
Int, for String, for
anything. The
next lesson
reads that line properly.
A function as an argument
twice is a higher-order function: one of its
arguments is itself a function. Nothing special is needed to pass one; you
write its name where a value would go. The parentheses in the type,
(a -> a), are the only signal that the argument is
a function rather than a value.
A definition can also be built at the point of use, without a name, but that
is a lambda and it has its own
lesson.
Everything on this page uses named functions, which is the style to prefer
while the code is small.
Backticks and prefix form
An operator such as + is a function of two arguments
written between them. Wrapping its name in backticks turns any function into
an operator: 7 `div` 2 is exactly
div 7 2, and the last two lines of the program are
two spellings of the same division, both printing 3.
Both halves of the language are the same thing:
a + b is (+) a b, and
(`div` 2) is a section of div
with the second argument fixed. Backticks read better when the name is a
word, elem or div, and
prefix form is required when the function is passed to another one.
The offside rule
Haskell has no braces and no semicolons in ordinary code. A block is a group
of lines that start in the same column, and a line that starts further left
ends the block. That is the layout rule, usually called the offside rule
because a line is inside a block only while it stays to the right of the
keyword that opened it.
In the program above, the four print lines start in
the same column, so they are the four actions of the
do block, and main = do
starts to their left. Moving one line two spaces to the left would end the
block before it, and the result would be a parse error at the next line.
The compiler accepts an explicit form with braces and semicolons, which is
what layout desugars to, but it is written by tools and not by hand:
module Main (main) where
main :: IO ()
main = do { putStrLn "one"; putStrLn "two" }
That is one line where the layout version is three, and it is quoted here because generated code looks like this. In a file a person reads, one action per line and a consistent indentation are what the layout rule was designed for.
Exercises
- Write square :: Int -> Int and sumSquares :: Int -> Int -> Int and print sumSquares 3 4. What is the result, and what does the name of the function tell a reader?
- Change twice double 5 to twice (double . double) 5 and then to twice double (twice double 1). Explain both results before you run them.
- Remove the parentheses in double (add 1 1) and read the error. Which type does the compiler expect for the first argument of double?
- Indent one of the print lines in the do block two spaces to the left and build. What line does the parser complain about, and why is it that one?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How is a function called in Haskell?
Answer
With a space between the name and each argument: add 2 3
Application is a space, and it binds tighter than any operator; parentheses are only needed around an argument that is itself a call.
-
What does
f a bmean?Answer
(f a) called with b
Application is left-associative, so f a b is (f a) b.
-
What does the type
(a -> a) -> a -> asay about twice?Answer
It takes a function from a type to the same type, then a value, and returns a value
The lowercase a is a type variable, and both occurrences in (a -> a) must be the same type, which is what makes twice work for any type.
-
Which expression means the same as
div 7 2?Answer
7
div2Backticks turn a function name into an infix operator with the left operand first, so 7
div2 is div 7 2. -
Two lines of a do block start in the same column, and a third starts further left. What does that mean?
Answer
The third line is outside the block
Layout ends a block at the first line that starts to the left of the block, so the third line is outside it.
-
What does this program print?
module Main (main) where add :: Int -> Int -> Int add a b = a + b main :: IO () main = print (add 2 3)Answer
5
add 2 3 is 2 + 3, which print shows as 5.
-
What does this program print?
module Main (main) where double :: Int -> Int double x = x * 2 twice :: (a -> a) -> a -> a twice f x = f (f x) main :: IO () main = print (twice double 5)Answer
20
twice double 5 is double (double 5), and both calls double the number, so the result is 20.
-
What does this program print? ``
module Main (main) where main :: IO () main = print (7div2 :: Int)``Answer
3
Integer division throws away the remainder, and print shows the Int as 3.
-
What is the first line of the compiler output for this program?
module Main (main) where add :: Int -> Int -> Int add a b = a + b double :: Int -> Int double x = x * 2 main :: IO () main = print (double add 1 1)Answer
error: [GHC-83865]
Without parentheses, double is applied to three arguments while its type takes one, so the types do not match and GHC reports GHC-83865.
-
Which definition is written with a correct type signature?
Answer
add :: Int -> Int -> Int
A function of two arguments has two arrows, one before each argument type and one before the result.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev