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
What is the purpose of partial function application?
It allows the processing of multiple arguments by a function.
What is meant by the head of a list in functional programming?
The first item in the list.
What is meant by the tail of a list in functional programming?
All the items in the list apart from the first.
How many data types can a list in a purely functional language contain?
1
In functional programming languages, data structures like lists are immutable. What does this mean?
The list cannot be changed once created.
Two functions are defined as follows f x y = x + y and g x = x * x. Why is it not possible to combine these functions as f.g?
Because f takes two arguments and g only takes one.
What is a higher order function?
A function which takes a function as an argument, returns a function as a result, or does both.
What is an argument?
The pieces of data that are passed into a function
Give an example of type declaration of a function for multiplying two integers
MultiplyUs : : : int –> int –> int
the first two int’s tell us the argument type, the last int tells us the result type
Give an example of declaring a function for multiplying two integers
MultiplyUs x y = x * y
for example MultiplyUs 3 5 would return 15
How are partial function applications implemented?
One of the arguments is fixed, leading to a more restricted, specialised function.
What is functional composition?
- The process of combining two functions to create a new function
What is the benefit of functional composition?
User’s cane use the functions both separately, and in conjunction.
What is required of functions that are combined?
One function’s domain (data type and number of arguments) must be the same as the other functions Co-Domain
What do higher order functions use?
They use recursion
What is function application?
The process of giving a function a particular input
In functional programming, programs are stateless, what does this mean?
That when a program is running, the variables cannot change (e.g. they cannot change state)