12.1 Functional programming paradigm Flashcards
What is a function?
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.
How do functions work?
An argument (a piece of data) is passed to a function, and the rule is applied to the argument, creating the return value (the output). An argument could be a number, a character or any other data type. The argument could be even be another function (if the language supports it). The function will specify what data type is required for the argument.
What is an argument?
The actual value passed into the parameter when the function is called
What is a function type using f is the function A as the input and B is the output?
The function type can be defined as the following:
f: A -> B
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 set of inputs (A) as the co-domain. The domain and co-domain are always subsets of objects in some data types
What is the domain of a function?
The set of all possible inputs for the function.
What is the co-domain of a function?
A set from which the function’s output values are chosen
What are first class objects?
Objects which may:
- Appear in expressions
- Be assigned to a variable
- Be assigned as arguments
- Be returned in function calls
Give examples of first class objects?
Integers, floating-point values, characters and strings.
In functional programming what are functions considered as and explain why?
They are considered as first-class objects. In some imperative languages they can be supported as first-class objects are supported meaning they can be passed as arguments or returned as the result of another function.
What does function application mean?
A term for applying the function rule to the arguments of the function.
What is partial function application?
One of the arguments is fixed, leading to a more restricted, specialised function.
What is functional composition?
The act of combining two functions to create a new function.
What are the benefits to functional composition?
The user is able to use the functions both separately and in conjunction.
When can 2 functions be combined?
As long as the domain of one of the functions is the same as the co-domain of the other.