12.1. Functional Programming Paradigm - Intermediate
12.1. Functional Programming Paradigm - Intermediate
www.pmt.education
Specification:
www.pmt.education
4.12.1.3 Function application
Know that function application means a function applied to its
arguments.
The process of giving particular inputs to a function is called function
application, for example add(3,4) represents the application of the function
add to integer arguments 3 and 4. The type of the function is f: integer x
integer → integer where integer x integer is the Cartesian product of the set
integer with itself. Although we would say that function f takes two
arguments, in fact it takes only one argument, which is a pair, for example
(3,4).
www.pmt.education
Functions
A function is defined as a rule that takes each member of a set of inputs and returns an
output from a set of possible outputs.
An argument is the name given to the pieces of data that are passed to a function. An
argument could be a number (0, 1, 3.4, -8 e.t.c), a character (“a”, “D”, “!” e.t.c) or any
other data type. The function will specify what data type is required for the argument.
Function Example 1:
A function is called DoubleMe . This outputs the double of
the input. The inputs (set A) could be the set of natural
numbers {0, 1, 2, 3, 4….}, and therefore the output (set B)
could be the set of even natural numbers {0, 2, 4, 6, 8 ….}
(as doubling a value always results in an even number).
The function DoubleMecould be called with a value of 6. The output would be the
double of 6, which is 12. 6 is called the argument, and 12 is the result. Here is the code
for such an interaction.
DoubleMe 6
>> 12
Function Example 2:
A function is called HalfMe . This outputs half of the input.
The input (Set A) could be the set of positive integers {0, 1,
2, 3, 4….} and the output (Set B) could be drawn from the
set of real numbers. Most of the real numbers will never be
outputted (e.g. 3.201 is not half of an integer), but the real
numbers is still an appropriate set. Below are two examples
of how HalfMemight be used.
www.pmt.education
Function Types
All functions have a function type. If f is the function, A is the input and B is the output,
the function type can be defined as the following:
A is known as the argument type, and B is the result type. This means function f maps A
to B. In Computer Science, we describe the the set of inputs (A) as the domain, and the
set of outputs (B) as the co-domain. Not all members of the co-domain have to be used
as outputs. The domain and co-domain are always subsets of objects in some data
type, as further explained below.
The set of natural numbers is the domain and the set of even natural numbers is the
co-domain.
In this example, the programmer only allows positive integers to be doubled. This
function could not be used to double a negative number or a decimal value.
The domain is a subset of the natural numbers, and a proper subset of the integers and
reals. The co-domain is a proper subset of the naturals, integers and reals.
If f was declared with this function type any real number could be doubled including
negatives and decimal values.
www.pmt.education
Function Types Example 2:
The function g returns half of the input. It’s function type could be described as below:
The set of natural numbers is the domain and the set of reals is the co-domain.
This function can only be used to half positive integers. Therefore not all of the reals
could be used as outputs (e.g. negative numbers).
The domain is a subset of the natural numbers, and a proper subset of the integers and
reals. The co-domain is a subset of the reals.
If the programmer wanted to only half integers, the domain could be the set of integers.
www.pmt.education
First-class objects
Function application
Function application is just a fancy term for applying the function rule to the arguments
of the function.
The first two “int”s signify the inputs, and the last int is the output.
www.pmt.education
Next we need to specify what the function will be doing with the inputs.
The x and y represent the parameters - variables created when the function is declared,
into which the arguments (data) is passed.
Now we are ready for function application. In this instance, the arguments are 3 and 5.
Although it may look like the multiply function is taking two arguments, it is in fact only
taking one - a pair. Every function in a functional programming language (e.g. Haskell)
only takes one argument. How can this be true? If we look back at the type declaration,
it clearly has two inputs. However, the type declaration can also be written as this:
Now we have two functions, each with one input and one output.
www.pmt.education
Function 1 is called first.
As you can see, the first function has created a new function based off the input. The
output of MultiplyUs 3 is MultiplyYBy3 5 (remember in this example y = 5). This new
function is called.
The output is 15. All inputs have been dealt with so MultiplyUs 3 5 returns 15.
www.pmt.education
Partial function application
Partial function application takes advantage of the inability of a function to take more
than one input. In partial function application, one of the arguments is fixed, leading to a
more restricted, specialised function.
In this case, the inputs and outputs are real. This function can take decimals and
negative numbers as arguments. Add3Numbers could be called as thus:
However, we could use partial function application to bind one of the arguments.
Consider the new function Add3ToTwoNumbers. This new function should take a pair of
arguments, add them together and then add 3, before returning the output. We could
write a new definition for this function, or we could use partial function application to
modify Add3Numbers.
Calling Add3Numbers 3 would ordinarily cause an error, as there are not enough inputs.
However, by defining Add3ToTwoNumbers in terms of Add3Numbers,
Add3ToTwoNumbers can be called with two additional inputs.
www.pmt.education
Composition of functions
Functional composition is the process of combining two functions to create a new
function. The benefit of this is that the user is able to use the functions both separately,
and in conjunction. Any two functions can be combined as long as the domain of one of
the functions is the same as the co-domain of the other. The symbol ○ indicates that
two functions are being combined e.g. Add2 ○ DoubleMe. The function works from the
inside out. Hence in the previous example, the input would be doubled first, then 2
would be added to it.
To be combined, the domain of one function must be the same as the co-domain of the
other function.
The domain of f is a pair of reals; the co-domain of g is one real. Therefore they cannot
be combined as f ○ g.
The domain of g is a real; the co-domain of f is also a real. Therefore they can be
combined as g ○ f.
www.pmt.education
The domain of g ○ f is the domain of f (a pair of reals), and the co-domain of g ○ f is
the co-domain of g (a single real).
www.pmt.education