Know that a function, f, has a function type
f: A → B (where the type is A → B, A is the argument type, and B is the result type).
Know that A is called the domain and B is called the co-domain.
Know that the domain and co-domain are always subsets of objects in some data type.
A function type denotes the kind of inputs that a function can take and the kind of outputs which the function will give.
For example, an add function may look something like this:
add :: Int -> Int -> Int
add x y =
x + y
This function takes two integers and returns an integer. You can tell this from the type definition before the function.
add :: Int -> Int -> Int
sumToString :: Int -> Int -> String
When talking about the inputs a function can take and the outputs a function can give, we often refer to Domain and Co-domain.
The domain is the set of values from which the function is allowed to take values as inputs.
Example 1 and example 2 allow for an input which belongs to the set of Integers.
The co-domain is the set from which a functions outputs are chosen.
Example 1 shows that the output of the function will have a type of Int and therefore belong to the Integers.
In example 2 however, the co-domain is different from the domain.
The co-domain of example 2 is a type of String
, meaning that the function will output a string.
Loosely speaking, a function is a rule that, for each element in some set A of inputs, assigns an output chosen from set B, but without necessarily using every member of B. For example,
f: {a,b,c,…z} → {0,1,2,…,25} could use the rule that maps a to 0, b to 1, and so on, using all values which are members of set B.
The domain is a set from which the function’s input values are chosen.
The co-domain is a set from which the function’s output values are chosen. Not all of the codomain’s members need to be outputs.