AQA A-Level Computer Science 7517
Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation
Fundamentals of Data Representation
Fundamentals of Computer Systems
Fundamentals of Computer Organisation and Architecture
Fundamentals of Communication and Networking
Consequences of Uses of Computing
Systematic Approach to Problem Solving
Fundamentals of Databases
Big Data
Fundamentals of Functional Programming
Partial Function Application and Composition
- 3 things to know
- Paper 2
Overview
Recall questions on partial function application and how its types are written and grouped, practical reasons to use it, and composing functions and working out g ○ f and f ○ g.
What you need to know
Answer each one before you reveal it.
A three-argument function volume multiplies three integers. Give its partial function application type, and describe what each successive application produces.
Given f: A → B and g: B → C, why does g ○ f have domain A and co-domain C?
Questions from this subtopic
Each one with the model answer underneath.
- 1
What does it mean to apply a function partially?
Model answer
Partial function application means applying a function to fewer arguments than it finally needs, so that instead of a final result it returns a new function which takes the remaining arguments. The arguments are supplied one after another rather than all at once, and each application returns something that can be applied again until the last argument arrives and an ordinary value is produced.
- 2
Write the type of the two-argument function add in the partial function application scheme, and explain what add 4 gives.
Model answer
Under that scheme the type is add: integer → (integer → integer). Read it from the outside in: add accepts one integer and gives back something whose own type is integer → integer, which is to say a function. So supplying just 4 produces no number at all; it produces a new function that stands ready to add 4 to whatever integer is handed to it next. Applying that result is a second and separate step, which is why (add 4) 7 evaluates to 11 — the first application builds the adder, the second uses it.
- 3
The brackets in add: integer → (integer → integer) may be dropped. Write the result and explain why it means the same thing.
Model answer
It becomes add: integer → integer → integer, and it means the same thing because the arrow groups to the right by convention. A chain of arrows is always read from the right-hand end, so integer → integer → integer must be understood as integer → (integer → integer); the reading (integer → integer) → integer is never what the notation means, and would describe something quite different, a function whose own argument is a function. Written without brackets, the type reads naturally as add consuming its arguments one at a time, each application swallowing a single integer, until at last an integer result emerges.
- 4
Why is it important that the arrow groups from the right rather than the left?
Model answer
Because the two readings describe entirely different functions. Read from the right, integer → (integer → integer) is a function that takes an integer and gives back a function — this is add in the partial application scheme. Read from the left, (integer → integer) → integer would be a function whose argument is itself a function from integer to integer and whose result is a single integer, which is a higher-order function of a quite different kind. The convention exists so that partial application can be written without brackets, which is the far commoner case.
- 5
Write the partial function application type of a one-argument function, and explain why nothing changes for it.
Model answer
A one-argument function such as double: integer → integer already has just one arrow, so there is only one argument to supply and applying it produces the result immediately. Partial application has nothing to do here, because there are no remaining arguments for an intermediate function to wait for. The scheme only becomes interesting from two arguments upwards.
- 6
Give the partial function application type of a three-argument function that multiplies three integers, and describe what each successive application produces.
Model answer
volume: integer → integer → integer → integer, which with brackets restored is integer → (integer → (integer → integer)). Applying volume to one integer, as in volume 2, returns a function of type integer → integer → integer. Applying that to a second integer, as in volume 2 3, returns a function of type integer → integer. Applying that to a third, as in volume 2 3 4, finally returns the integer 24. Each application removes exactly one arrow from the left of the type.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Fundamentals of Functional Programming, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.