Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 57 ยท Getting started

Expressions, Functions and Application

Learn how Haskell treats everything as an expression, how to call and define functions without parentheses, use infix operators and write operator sections.

In many languages a program is a list of statements that do things. In Haskell a program is built from expressions, which are pieces of code that have a value. Even if produces a value. Functions are expressions too, and calling one is written with nothing more than a space.

This lesson covers expressions, function application, defining your own functions, the difference between prefix and infix calls, and operator sections. These are the daily tools of every Haskell program.

Everything is an expression

An expression evaluates to a value of some type. Numbers, strings, calls and conditionals are all expressions:

HASKELL
42                          -- 42
"hi"                        -- "hi"
3 + 4 * 2                   -- 11
if 5 > 3 then "yes" else "no"   -- "yes"

Because if ... then ... else ... is an expression, it always needs an else branch, and both branches must have the same type. There is no "do nothing" branch. The same is true of case expressions, which you meet in pattern matching.

You can use any expression wherever a value is expected:

HASKELL
label :: Int -> String
label n = "You have " ++ (if n == 1 then "one item" else show n ++ " items")

Function application

To call a function, write its name and then its arguments, separated by spaces:

HASKELL
max 3 7          -- 7
min 10 2         -- 2
succ 'a'         -- 'b'

There are no commas and no required parentheses. In C-like languages you would write max(3, 7). Here, max 3 7 is already the complete call.

Function application is the tightest-binding thing in the language. It binds more strongly than any operator:

HASKELL
max 3 7 + 1      -- (max 3 7) + 1 = 8, not max 3 (7 + 1)

When an argument is itself a call, parenthesize it:

HASKELL
max 3 (min 10 20)   -- 10
-- max 3 min 10 20  -- wrong: max would receive four arguments

Application also associates to the left: f a b means (f a) b. That detail is the key to currying, which comes soon.

Reading a call

f x y Call f with two arguments.
Tightest binding f x + 1 means (f x) + 1.
Nested calls Wrap an argument call: f (g x).

Spaces are the call operator.

1 / 2

Defining functions

A definition is a name, its parameters and an expression after =. A type signature above it is good practice:

HASKELL
square :: Int -> Int
square x = x * x

area :: Double -> Double -> Double
area width height = width * height

greet :: String -> String
greet name = "Hello, " ++ name ++ "!"

There is no return. The function's result is the expression on the right. Names of functions and variables start with a lowercase letter; types and constructors start with uppercase.

A function with no parameters is just a constant (a definition):

HASKELL
secondsPerDay :: Int
secondsPerDay = 24 * 60 * 60

Definitions can be used in any order inside a module; Haskell does not care which comes first. Names may contain letters, digits, underscores and apostrophes: x', foldl' and step2 are all valid, and the apostrophe is often read "prime".

Prefix and infix

Operators such as + and ++ are just functions with symbolic names, written between their arguments (infix). Put one in parentheses and it becomes an ordinary prefix function:

HASKELL
3 + 4          -- 7
(+) 3 4        -- 7, the same call written prefix

The reverse also works: wrap an ordinary function in backticks to use it infix:

HASKELL
10 `div` 3     -- 3
10 `mod` 3     -- 1
div 10 3       -- 3, prefix again

Backticks often read like English: x elem [1, 2, 3]. You can define your own operators, and set their precedence with a fixity declaration:

HASKELL
infixl 6 |+|
(|+|) :: (Int, Int) -> (Int, Int) -> (Int, Int)
(a, b) |+| (c, d) = (a + c, b + d)

infixl 6 means left-associative with precedence 6, the same level as +. Precedence runs from 0 to 9; higher binds tighter. You can see the fixity of any operator in GHCi with :i (+).

Operator sections

A section is an operator with one argument supplied, wrapped in parentheses. It gives you a function waiting for the missing argument:

HASKELL
(+ 1)       -- \x -> x + 1
(2 *)       -- \x -> 2 * x
(subtract 1) -- \x -> x - 1
(`div` 2)   -- \x -> x `div` 2

Note that (- 1) is not a section: it is the number negative one. Use subtract 1 for "minus one". Sections shine with map and filter, which you will meet in higher-order functions:

HASKELL
map (* 2) [1, 2, 3]        -- [2,4,6]
filter (> 2) [1, 2, 3, 4]  -- [3,4]

A JavaScript stand-in

JavaScript uses f(x, y) for calls and has no operator sections, but we can model them. This stand-in is not Haskell: JavaScript functions are not curried by default, so we write the sections by hand.

JS Calls, infix and sections

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The next model shows that if is an expression: a function that returns a value, with no empty branch.

JS if as an expression

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In real Haskell, if is evaluated lazily: only the chosen branch is computed. The ifThenElse function above evaluates both branch arguments first, since JavaScript is eager. The ternary ?: in the label example does behave like Haskell's if.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you call max with the values 3 and 7 in Haskell?

    Arguments are separated by spaces, with no commas or required parentheses.
  2. 2. What is the value of max 3 7 + 1?

    Function application binds tighter than any operator, so it means (max 3 7) + 1.
  3. 3. Why must an if expression in Haskell have an else branch?

    if is an expression with a value of one type, so both branches must exist and have the same type.
  4. 4. What does the section (2 *) mean?

    A section supplies one operand of an operator and yields a function that waits for the other.
  5. 5. Which line calls div in infix form?

    Backticks turn an ordinary function into an infix operator.

Summary

  • Haskell programs are made of expressions; even if yields a value and needs both branches.
  • Call a function by writing its arguments after it, separated by spaces; application binds tighter than operators.
  • A definition is name params = expression; there is no return.
  • Operators are functions: (+) 1 2 is prefix and a `div` b is infix.
  • Sections like (+ 1) and (2 *) build small functions on the spot; (- 1) is not a section, use subtract 1.

Next: Basic Types and Type Signatures.