AQA 7517, section 4.12.1.1
In functional programming, a program is built from functions, in the mathematical sense. Before using them, you need to understand what a function is and how its type is written.
What is a function?
A function is a rule that takes each possible input from a set and gives it exactly one output, which comes from a set . Some members of may never be produced as outputs.
Given the same input, a function always gives the same output.
Function type
A function has a function type, written
and read " is a function from to " (or " maps to ").
- is the argument type (the type of the input).
- is the result type (the type of the output).
| Part | Name | Meaning |
|---|---|---|
| Domain | The set from which the function's input values are chosen | |
| Co-domain | The set from which the function's output values are chosen |
The domain and co-domain are always subsets of the objects in some data type, such as integers, characters or strings.
Domain and co-domain
The domain is the set of values that the function can accept. The co-domain is the set that the outputs come from. Not every member of the co-domain has to be produced as an output.
Example 1
The rule maps a to 0, b to 1, c to 2, and so on, to z to 25. Here every member of the co-domain is used.
Example 2
The domain and co-domain are both the natural numbers. The outputs are only the even numbers, so most members of the co-domain (the odd numbers) are never produced. That's allowed: the co-domain says what type the result is, not that all values appear.
Example 3: more types
| Function | Type | Domain | Co-domain |
|---|---|---|---|
length of a string |
strings | integers | |
is_even |
integers | ||
square_root |
real numbers (0 or more) | real numbers | |
first_letter |
strings | characters |
Set notation and functions of two arguments
Exam questions often write the domain and co-domain using set symbols:
| Symbol | Set |
|---|---|
| Natural numbers: | |
| Integers: | |
| Real numbers |
So a function that subtracts 1 from an integer has the type .
A function with two arguments, such as on integers, takes a pair of integers. Its domain is the set of all pairs, written :
The co-domain is still , because the result is one integer. (Page 4.12.1.4 shows how the same function can also be written as , taking one argument at a time.)
Worked example
Task: write the function type of each, and give the domain and co-domain.
to_upper, which turns a string into capital letters.is_vowel, which says whether a character is a vowel.area, which takes a radius (a real number) and gives the area of a circle.
- (1) . Domain: strings. Co-domain: strings.
- (2) . Domain: characters. Co-domain: .
- (3) . Domain: real numbers (in practice 0 or more). Co-domain: real numbers.
Exam tip: in , is the argument type (domain) and is the result type (co-domain). Remember that not every co-domain member needs to be an output.
Check yourself
What does mean?
is a function that takes an argument from set (the domain) and gives a result from set (the co-domain).
What is the domain of a function?
The set from which its input values are chosen.
Must every member of the co-domain be an output of the function?
No. The co-domain is the set the outputs are chosen from, but not all of its members need to be produced.
Write the type of a function that takes a string and returns its length
.