Functional Programming Flashcards
What is a function type?
How would you write this?
It describes what the input and output values are.
E.g. inputting integers, outputting real numbers
Written like this: f: integer –> real (f: A –> B)
A is the argument type and B is the result type
What could changing the function type do?
The functionality could change without changing the instructions as different input values would be accepted.
Explain each part of this f: A –> B
F is the function name, A is the domain, B is the co-domain
What is the definition of a function in functional programming?
A rule that for each element in a set of inputs, assigns an output chosen from another set
What is the definition of a domain in functional programming?
A domain is a set from which a function’s input values are chosen.
What is the definition of a co-domain in functional programming?
A co-domain is a set from which the function’s output values are chosen.
What are the benefits of the functional programming paradigm?
- Supports the decomposition and abstraction of computing problems into functions that are made up of other functions
- Is suitable for running parallel and concurrent program execution, which allows programs to run on multiprocessor systems
What is a first-class object?
An object which can appear in an expression, be assigned to a variable, be assigned as an argument and be returned in function calls.
Not all combinations of functions are possible. Explain why this is.
A combination won’t work if:
- It doesn’t have the same number of inputs as outputs
- The type of the input isn’t the same as the type of the output
What is a higher order function?
- A function that can take functions as arguments or/and can return a function as a result
What does the map function do?
It applies a function to all the items in a list
What does the filter function do?
Returns the elements in the domain that satisfy a given condition.
What does the fold function do?
What is the difference between foldl and foldr?
Fold is also known as reduce
Reduces the list to a single value by applying a given operator to the values in the list. (usually adding)
Foldl means fold left, foldr means fold right for example: foldr (-) 100 {2,4,6,8,10) would equal -94 whereas foldl (-) 100 {2,4,6,8,10) would equal 70
Name the three first class objects in functional programming.
Functions
Integers
Variables
How many arguments can a haskell function take?
1, though it can look like 2 arguments are being taken, they are taken as a pair, which is treated as1 argument