Home » Teaching topics » Functional Programming » Applying functions

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8 other subscribers.

Applying functions

Functions are the basis of all processing carried out in functional programs. We give them arguments from the domain and they produce results from the co-domain. In the previous post we coded a function but now we will look in more detail at how they work in Haskell.

Consider an example function that takes an integer value as an argument and adds 10 to it. Firstly we should explicitly declare the function type that will be used by the function when accepting a parameter passed to it and then returning the result:

addTen :: Integer -> Integer

This declares that a function called addTen ‘will have type’ Integer -> Integer. What this actually means is it will take an Integer as an argument and map this value to an Integer result (the arrow shows the direction the mapping takes place from argument to result). Haskell could decide these types automatically but it is better for us to specify this before defining how the function will determine the correct result.

Next we define how the function should work:

addTen x = x + 10

Loading the source file into our interpreter we can test to make sure it works:

Prelude> addTen 2

By calling the function we have have carried out function application as defined in the AQA specification:

– Know that function application means a function applied to its arguments.

In Haskell functions only take one argument. We will see later that we can link functions together to process multiple arguments but we have to define this behaviour differently. If we want to define a function to act like it is accepting multiple values we have to specify a function type that accepts a Cartesian product of a set of integers with itself:

f: integer x integer → integer

A Cartesian product produces a array of ordered pairs of the form (row value, column value) for example:

Cartesian product example
The Cartesian Product of two sets A X B where A = B = {1,2,3}

If this is applied to sets of integers we can have as many different pairs as the data type allows. We are specifying that the function’s domain is this range of ordered pairs of values.

So how does all of this apply to the argument of a function? A Cartesian product of two elements gives us ordered pairs which are tuples of length 2. The function type above specifies that the domain is the Cartesian product of two sets of integers which allows us to call the function with an argument that is a tuple of two integer values. The tuple itself is a single argument but this allows us to use multiple values as part of the function definition:

divide :: (Double, Double) -> Double
divide (a, b) = a / b

…which can be called with an argument that is a tuple of two elements:

Prelude> divide (3,4)

We don’t have to be limited to this scheme and we should instead chain the results of functions together to achieve the same results. This will be covered in a later post and we will see that functions we create can be more flexible and powerful!

Leave a comment

Your email address will not be published. Required fields are marked *