Fundamentals of functional
programming
Last edited time @October 1, 2023 10:49 AM
Status Done
Function types
A function type is the way a function can be defined through its inputs and outputs.
These have been written below in the general form and Haskell respectively:
The domain is the set of possible inputs that a function can take. For the Haskell
example below, the domain would be the integers as the function returns the
square of a number. A domain is usually a set of numbers such as the integers, Z,
or reals, R, but they can include strings, such as in regex. More domains and co-
domains can be found at the page below.
🔒 Fundamentals of Data representation
The codomain is the set of possible outputs of a function. These can be numbers,
strings or even other functions.
myFunction :: Integer -> Integer
f :A→B
myFunction n = n ^ 2
where A = domain
and
B = co − domain
First-class objects and higher order functions
A first class objects is one that can be accepted as an argument into a
function or be returned by one. Some first class objects include, integers and
most notable other functions.
A higher order function is one that can take one or more functions as
arguments and then also return functions or both. Some important example
Fundamentals of functional programming 1
, are the map, fold, filter functions.
Partial and complete function application
Function application is the process of giving inputs to a function and it returning
the specified output. Take the example below:
volume = int * int * int -> int
We are supplying three parameters, which form the argument of the function.
Then the function returns an integer. Because all parameters have been supplied
and the integer is returned the function has been completely applied.
Partial Application
Partial application is the process of supplying some of the inputs to a function
which can then be used to create a useful output or intermediary step. Ie. Not all
parameters are given to a function at once. This allows for more versatile
functions. Take the following example:
We have defined 2 functions
Used both functions within Haskell, where one can be partially applied and
one cannot.
[Example 1] [Example 1]
add: int x int -> int add(x,y): add (2,3) = 5
[Example 2] [Example 2]
add: int -> int -> int add(x,y): add (2,3) = add2 3 = 5
Function composition.
Function composition is the process of combining simpler functions together to
form more complex ones. It works by applying the inner most function first and
that forms the argument for the next function. It is the same process that is done
within maths as with f(g(x)) = (x + 1)2 . This can be converted to code as
shown below:
-- Defining outer function
myFunc1 :: Integer -> Integer
Fundamentals of functional programming 2