Composition of functions — AQA A-Level Computer Science
Function composition is the process of combining two or more functions such that the output of one function becomes the direct input of the next. In AQA A-Level Computer Science functional programming, the composite function (f . g)(x) denotes f(g(x)), where g is evaluated first and its codomain must match the domain of f.
Composition of functions explained
Functional composition is an operation that takes two functions, f and g, and produces a third function that applies them in sequence.
Read the full explanation
The composition of f after g, written f ∘ g, is defined by (f ∘ g)(x) = f(g(x)): the input x is first passed to g, and the result of that is then passed to f. The output of g therefore becomes the input of f, so the two functions must be compatible, meaning the output type of g must fit the input type of f. Because the result is itself a function, composition can be chained and reused. For example, if g(x) = x + 1 and f(x) = 2x, then (f ∘ g)(3) = f(g(3)) = f(4) = 8, whereas (g ∘ f)(3) = g(f(3)) = g(6) = 7. This shows that composition is generally not commutative, so the order of the functions matters.
Your focus
- The operation functional composition combines two functions to get a new function.
Which command word is this?
Provide a concise answer without extensive explanation, such as writing a final type signature or numeric result.
Composition of functions exam tips
Topic Overview
Composition of functions is a core paradigm of functional programming within the AQA A-Level Computer Science specification. It involves combining two simpler functions to create a new, more complex function without manually writing intermediate variable assignments.
This technique allows software engineers to build clean, modular, and declarative programs by chaining operations together. Understanding functional composition strengthens computational thinking and links mathematical logic directly to practical programming constructs.
Key Concepts
- →Definition of composition: (f . g)(x) is mathematically defined as f(g(x)), executing g first and then f.
- →Type matching rule: Function composition (f . g) is only valid if the codomain of g is identical to or compatible with the domain of f.
- →Associativity: Function composition is associative, meaning (f . (g . h)) is equivalent to ((f . g) . h).
- →First-class functions: Composition relies on the fact that functions can be treated as first-class objects that can be accepted as arguments and returned from other functions.
Marking Points
- Identifies that the result of composition is a new function rather than a single value.
- States that the functions are applied in sequence, with one function's output used as the other's input.
- Recognises that f ∘ g applies g first and f second, which is the reverse of the order in which the notation is read.
- Notes that composition is generally not commutative, so f ∘ g and g ∘ f can give different results.
- Explains that the output type of the first function must match the input type of the second.
Examiner Tips
- 💡When evaluating a composed function at a point, work from the inside outwards, computing the innermost function first.
- 💡Check the order of the functions in the notation before calculating, since it determines the result.
- 💡Use a simple numerical example to test whether two functions commute if asked to justify a claim about order.
- 💡When asked to trace or evaluate composed functions, always write out the intermediate step f(g(x)) to demonstrate your working.
- 💡Always reference both 'domain' and 'codomain' when answering questions about whether a composition is valid.
- 💡Remember that the composition operator '.' produces a brand-new function; it does not evaluate until it receives its final argument.
Common Mistakes
- Treats composition as adding or multiplying the two functions; correct it by recognising that the functions are applied one after another to an input.
- Reverses the order of application in f ∘ g; correct it by applying the right-hand function first.
- Assumes f ∘ g and g ∘ f always produce the same result; correct it with a worked numerical example showing they can differ.
- Thinking that (f . g)(x) means f is evaluated before g; in functional programming, evaluation moves from right to left (inside to outside).
- Assuming any two functions can be composed regardless of their input and output types, ignoring domain/codomain type constraints.
- Confusing function composition with applying multiple arguments to a single curried function.
Revision Plan
- 1Day 1-2: Review functional programming basics, domains, codomains, and pure functions.
- 2Day 3-4: Practise converting expressions between f(g(x)) and (f . g)(x) with numeric functions.
- 3Day 5-6: Solve problems involving type matching and non-numeric types (e.g., String, Boolean, Lists).
- 4Day 7: Complete past exam questions on tracing and defining composite functions under timed conditions.
Exam Question Types
- 📋Evaluation questions: Given function definitions and an input, trace and calculate the final return value of a composite function.
- 📋Type signature deduction: Given two functions with defined types, write out the type signature of their composition.
- 📋Validity explanation: Determine whether two given functions can be composed and explain why using domain and codomain terminology.
Command Word Expectations (AQA)
Provide a concise answer without extensive explanation, such as writing a final type signature or numeric result.
Give reasons or causes for a statement, such as justifying why a composition is valid with reference to domain and codomain rules.
Perform mathematical or logical evaluation step by step to arrive at a definitive final value.
How Students Lose Marks (Examiner Pitfalls)
Step-by-Step Worked Solutions
Question: Two functions are defined as follows: inc :: Int -> Int inc x = x + 1 dbl :: Int -> Int dbl x = x * 2 Calculate the result of (inc . dbl) 4 and explain why the composition is valid with reference to types.
- 1.Step 1: Identify the order of execution. The composite function (inc . dbl) 4 is equivalent to inc(dbl(4)).
- 2.Step 2: Evaluate the inner function first: dbl(4) = 4 * 2 = 8.
- 3.Step 3: Evaluate the outer function using the result of Step 2: inc(8) = 8 + 1 = 9.
- 4.Step 4: Check type compatibility. dbl has the signature Int -> Int, meaning its codomain is Int. inc has the signature Int -> Int, meaning its domain is Int. Since the codomain of dbl matches the domain of inc, the composition is valid.
Question: Consider the functions: isEven :: Int -> Bool isEven n = (n `mod` 2 == 0) strLen :: String -> Int strLen s = length s Write the type signature of the composed function (isEven . strLen) and evaluate (isEven . strLen) 'Mastery'.
- 1.Step 1: Determine the domain and codomain of each function. strLen takes a String and outputs an Int. isEven takes an Int and outputs a Bool.
- 2.Step 2: Check type compatibility. The output of strLen (Int) matches the input of isEven (Int).
- 3.Step 3: Formulate the type signature of the composite function. Input is String, final output is Bool, so (isEven . strLen) :: String -> Bool.
- 4.Step 4: Evaluate the expression on the argument 'Mastery'. First evaluate strLen('Mastery') = 7.
- 5.Step 5: Apply isEven to the intermediate result: isEven(7) = False.