AS/A Computer Science: Function application

What is function application?

The process of giving particular inputs to a function is called function application e.g. add(3,4) represents the application of the function add to the integer arguments 3 and 4. The type of the function is
f: integer x integer → integer
where integer x integer(said integer cross 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).

We can define functions in Haskell and apply the function to specific arguments and have the result returned.



Partial function application

Any function only takes one argument at a time. The function add takes two integers as arguments and gives an integer as a result but can be viewed as follows in the partial function application scheme:
add: integer → (integer → integer)
When we call add 3 4 add 3 returns a function which when applied to 4 adds 3 to 4. The brackets may be dropped so function add becomes
add: integer → integer → integer
The function add is now viewed as taking one argument after another and returning a result of data type integer.

Because functions are evaluated one after the next we can create functions that take advantage of this e.g.
addTen = add 10
We now supply one argument and addTen provides the other.



Composition of functions

The operation functional composition combines two functions to get a new function. Given two functions
f: A → B
g: B → C
function g o f, called the composition of g and f, is a function whose domain is A and co-domain is C.
If the domain and co-domains of f and g are ℝ, and f(x) = (x + 2) and g(y) = y3, then
g o f = (x + 2)3
f is applied first and then g is applied to the result returned by f.

Below you can see the result of addtwo 3 is used as the argument for timesthree. When we pass timesthree(addtwo 3) it first adds 2 to 3 to get 5 and then multiplies 5 by 3 to get 15.



Knowledge check


Questions:
Correct:

Question text


© All materials created by and copyright S.Goff