Revision Notes
AQA A-Level Computer Science (7517): Functional Programming Paradigm – Revision Notes
Condensed AQA 7517 section 4.12.1 recall: function type, domain, co-domain, first-class objects, function application, partial application and composition.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Fundamentals of functional programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.12.1 Functional programming paradigm
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.12.1 Functional programming paradigm, AQA A-Level Computer Science (7517): function type, first-class objects, function application, partial function application and composition. Worked evaluations are in the functional programming paradigm study guide; questions are in the functional programming paradigm practice questions.
Function type
f : A → B
| Term | Meaning |
|---|---|
| Argument type | A, the type of the input |
| Result type | B, the type of the output |
| Domain | The set from which the inputs are chosen (A) |
| Co-domain | The set from which the outputs are chosen (B) |
- Domain and co-domain are always subsets of objects in some data type.
- Not every member of the co-domain must be an output.
- A function assigns one output to each input in the domain.
| Function | Type |
|---|---|
length |
string → integer |
isPositive |
integer → Boolean |
toUpper |
character → character |
First-class objects
A first-class object can:
- appear in expressions;
- be assigned to a variable;
- be passed as an argument;
- be returned from a function call.
In functional languages (and imperative languages that support them) functions are first-class, so a function can be an argument to another function and the result of a call. Integers, floats, characters and strings are first-class in most languages.
Check: twice f x = f (f x). With double x = 2 * x: twice double 5 = double (double 5) = 20.
Function application
Applying a function to its arguments: add(3, 4) = 7.
add : integer × integer → integer
integer × integeris the Cartesian product: all pairs of integers.- Strictly,
addtakes one argument, a pair such as (3, 4).
Partial function application
add : integer → integer → integer means add : integer → (integer → integer).
add 4is a function of type integer → integer (“add four to the input”).(add 4) 5= 9.→associates to the right, so brackets can be dropped.
| Expression | Type | Value |
|---|---|---|
volume (l, w, h) |
integer → integer → integer → integer | the function |
volume 2 |
integer → integer → integer | needs w and h |
volume 2 3 |
integer → integer | needs h |
volume 2 3 4 |
integer | 24 |
Rule: each argument supplied removes one type from the front of the type. The number of arrows left is the number of arguments still needed.
addTen = add 10, then addTen 5 = 15. map (add 10) [1, 2, 3] = [11, 12, 13].
Composition
Given f : A → B and g : B → C:
g ∘ f : A → C and (g ∘ f)(x) = g(f(x))
- f is applied first, then g. Read right to left.
- The result type of f must match the argument type of g.
- Order matters.
Check: f(x) = x + 2 and g(y) = y³. (g ∘ f)(1) = g(3) = 27. (f ∘ g)(1) = f(1) = 3.
Type check: isEven ∘ length : string → Boolean. length ∘ isEven is not defined, because isEven returns a Boolean.
Pure function
A pure function gives the same result for the same arguments and has no side effects. This is why functions can be applied, partially applied and composed freely.
Imperative versus functional
| Imperative | Functional | |
|---|---|---|
| Main building block | Sequence of statements changing variables | Functions applied to values |
| State | Variables are changed in place | Values are not changed; new values are returned |
| Repetition | Loops | Recursion and higher-order functions |
| Functions | May have side effects | Pure; functions are first-class |
Key terms
| Term | Definition |
|---|---|
| Function type | The type A → B, showing argument type and result type |
| Domain | The set of input values |
| Co-domain | The set from which output values are chosen |
| First-class object | A value that can be used in expressions, assigned, passed and returned |
| Function application | Giving particular inputs to a function |
| Partial application | Supplying fewer arguments than a function needs, giving a new function |
| Composition | Combining two functions so that the output of one feeds the next |
| Cartesian product | The set of all pairs of elements from two sets |
| Higher-order function | A function that takes or returns a function |
Exam traps
- Domain is the input set; co-domain is the output set.
- Not all co-domain members need to be outputs.
- g ∘ f means f first.
- Count arrows to find how many arguments remain.
- Types must match in a composition.
- Remove arguments from the front of the type in a partial application.
- Do not write
add (3, 4)for the curried form; useadd 3 4.
Self-test
- In f : A → B, what is A called?
- In f : A → B, what is B called?
- Must every member of the co-domain be an output?
- State two things a first-class object can do.
- Give the type of an addition function on integers in partial application form.
- What is the type of
add 4? - What does
(add 4) 5evaluate to? - What is the type of
volume 2 3if volume : integer → integer → integer → integer? - In g ∘ f, which function is applied first?
- If f(x) = x + 2 and g(y) = y³, what is (g ∘ f)(2)?
- What is (f ∘ g)(2) for the same functions?
- What condition must be true for g ∘ f to be defined?
Answers
- The domain (argument type).
- The co-domain (result type).
- No.
- Any two of: appear in expressions; be assigned to a variable; be passed as an argument; be returned from a function call.
- integer → integer → integer.
- integer → integer.
-
- integer → integer.
- f.
- g(4) = 64.
- f(8) = 10.
- The result type of f must match the argument type of g.
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Lists in Functional Programming
Deeper guide to AQA 7517 section 4.12.3: lists as a head and a tail, the empty list, and the operations head, tail, empty test, length, prepend and append, with recursive definitions.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Lists in Functional Programming – Practice Questions
Original AQA 7517 section 4.12.3 questions with answers: head and tail, the empty list, list operations and writing and tracing recursive list functions.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Lists in Functional Programming – Revision Notes
Condensed AQA 7517 section 4.12.3 recall: head and tail, the empty list, the seven list operations and recursive definitions of length, append, sum and reverse.
Computer Science · AQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.