๐Ÿ“˜ CodingMarble Learn

Functional Programming: Functions, Map, Filter and Fold

In functional programming a program is built from functions. A function maps each input from its domain to one output in its co-domain, and its type is written f: A โ†’ B. Pure functions give the same output for the same input and change nothing else (no side effects); data is immutable. Functions are first-class: they can be named, passed in and returned. Partial application gives a function some of its inputs and returns a new function. Composition g โˆ˜ f runs f, then g. Higher-order functions take or return functions: map applies a function to every list item, filter keeps items that pass a test, fold (reduce) combines a list into one value. A list is a head (first item) plus a tail (the rest).

๐ŸŽฌ Step-by-step story

  1. A function is a machine. Put 3 into square and 9 comes out. Its type is integer โ†’ integer: the domain and the co-domain.
  2. Give add only one input, 5. You get a new function, add5. This is partial application. Functions are values too.
  3. Join two machines: first add 1, then double. 3 becomes 4, then 8. This is composition, written g โˆ˜ f.
  4. map squares every item in the list. filter keeps only the even ones. The old list never changes.
  5. A list is a head plus a tail. fold adds every item into one total: 1 + 2 + โ€ฆ + 6 = 21.
  6. Your turn: pick map, filter or fold, change the list length, and predict the output before you press.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

๐Ÿค” Common doubts, cleared

Is a function the same as a procedure in Python?

Not always. A mathematical function returns one value and changes nothing else; a procedure may print or change variables. Step 1 shows the pure machine.

How can a function return another function?

Give add only its first input and it hands back add5, a new machine waiting for the second input. Step 2 shows this.

In g โˆ˜ f, which runs first?

f runs first, then g. Watch the ball pass through the blue f machine first in step 3.

Does map change the original list?

No. The blue row stays; a new green row is made. Step 4.

What is the tail of a one-item list?

The empty list []. The tail is always a list, never a single item. Step 5 shows head and tail.

Why does fold need a start value?

It is the answer for an empty list and the first value the total grows from. Try fold with the list length set to 1 in step 6.

What is the functional paradigm?

A programming paradigm is a style of writing programs. In imperative programs you give step-by-step orders that change variables. In functional programs you describe the answer as functions applied to data.

Function, domain and co-domain

A function links every input to exactly one output. The set of allowed inputs is the domain. The set that outputs are taken from is the co-domain. We write the function type as f: A โ†’ B, meaning f takes a value from set A and gives a value in set B.

Example: isEven: integer โ†’ Boolean. Domain = integers, co-domain = {True, False}.

Pure functions and no side effects

Why it helps: code is easier to test, easier to prove correct, and safe to run on many processors at once.

First-class functions, partial application and composition

First-class functions

A function is first-class when it can be used like any other value: given a name, passed as an argument, returned as a result, and stored in a list.

Higher-order functions

A higher-order function takes a function as an argument or returns one. map, filter and fold are higher-order.

Partial application

Think of add x y = x + y as having type integer โ†’ (integer โ†’ integer). If you give it only 5, you get a new function add5 = add 5 of type integer โ†’ integer. Then add5 2 = 7.

Function composition

g โˆ˜ f means apply f first, then g: (g โˆ˜ f) x = g(f(x)). If f: A โ†’ B and g: B โ†’ C then g โˆ˜ f: A โ†’ C. The co-domain of f must match the domain of g. Order matters: usually g โˆ˜ f โ‰  f โˆ˜ g.

Lists: head, tail and list operations

In functional languages a list is either empty [], or a head (the first item) joined to a tail (a list of the rest).

Because a list is head + tail, many list functions are written with recursion: do something with the head, then call the same function on the tail, and stop at the empty list.

total [] = 0
total (x:xs) = x + total xs

Map, filter and fold (reduce)

They can be chained: sum of squares of even numbers in [1..6] = fold (+) 0 (map (^2) (filter even [1..6])) = 4 + 16 + 36 = 56.

Try it

In the last 3D step, choose map, filter or fold and slide the list length. Say your prediction aloud first. Then try in Python: list(map(lambda x: x*3, [1,2,3])), list(filter(lambda x: x%2, [1,2,3,4])) and functools.reduce(lambda a,b: a+b, [1,2,3], 0).

Key formulas and definitions

Worked examples

1. State the type, domain and co-domain of isEven, which tells whether an integer is even.

isEven: integer โ†’ Boolean. Domain = integers. Co-domain = {True, False}.

2. f x = x + 3 and g x = x ร— 2. Find (g โˆ˜ f) 4 and (f โˆ˜ g) 4.

(g โˆ˜ f) 4 = g(f 4) = g(7) = 14. (f โˆ˜ g) 4 = f(g 4) = f(8) = 11. Different answers, so order matters.

3. mult x y = x ร— y. What is triple = mult 3, and what is triple 5?

mult 3 is a partially applied function waiting for y; triple: integer โ†’ integer. triple 5 = 15.

4. Evaluate head (tail [8, 3, 6, 1]).

tail [8,3,6,1] = [3,6,1]; head [3,6,1] = 3.

5. Evaluate map (+1) (filter odd [1,2,3,4,5]).

filter odd gives [1,3,5]; map (+1) gives [2,4,6].

6. Evaluate fold (โˆ’) 20 [5, 3, 2] as a left fold, and the sum of squares of [1,2,3] using map and fold.

Left fold: ((20โˆ’5)โˆ’3)โˆ’2 = 10. Sum of squares: map (^2) [1,2,3] = [1,4,9]; fold (+) 0 [1,4,9] = 14.

Common mistakes

Practice quiz

1. Which function returns a list of the same length as its input list?
2. tail [5, 9, 2] is:
3. A function that takes a function as an argument is called:
4. If f x = x ร— x and g x = x โˆ’ 1, (g โˆ˜ f) 3 equals:
5. fold (+) 0 [2, 4, 6] equals:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is functional programming in simple words?

It is a way of programming where you build the answer by joining pure functions that turn inputs into outputs, without changing variables or data.

What is the difference between map, filter and reduce?

map changes every item, filter removes items that fail a test, and reduce (fold) combines all items into a single value.

What is partial application?

Giving a function fewer arguments than it needs, which returns a new function that waits for the rest, such as add 5 giving add5.

Where this is taught

England (GCSE, A level)Year 134.12 Fundamentals of functional programming

Learn first

Learn next

Related lessons

All Computer Science lessons