What do side-effects do?
The potential for side-effects means you cannot guarantee that a given function will provide exactly the same output for a given input.
What is referential transparency?
A property of functions in which the output value is determined solely by its input values, without any side effects or dependence on external state.
What is a function?
A rule (or set of rules) that maps each element from the input set A (the function’s domain) to a particular value from its output set B (the function’s co-domain).
What is the image of a function?
The subset of values from the co-domain that are mapped to as outputs.
What are types?
Sets of values (determines what kind of data a value is able to store)
What is a function classified as in functional program?
Functions are first-class objects, meaning that they can be used just as literal values of of other types / classes.
For example:
- Functions can appear in expressions
- Functions can be assigned to variables
- Functions can be passed as an argument to another function
- Functions can be returned as the result of a function.
What is a higher-order function? (MS)
A function that takes a function as an argument; and/or returns a function as a result;
How to define a function in functional programming?
1) Declare the function’s type using a function signature
2) State the function’s expression that will map its input arguments to output values
E.g.
AddNums :: Int -> Int -> Int
AddNums x y = x + y
What is the definition of function appliction?
Function application means to apply a function to its argument and can be described as the process of giving particular inputs to a function.
What is the requirements for two functions to be combined?
What is pattern matching and how is it used in defining functions in functional programming?
Pattern matching is used to define multiple function bodies depending on the function’s argument value.
E.g.
Factioral :: Int -> Int
Factorial 0 = 1
Factorial n = n * Factorial (n-1)
What is a bound variable in functional programming?
Usually n, usedd to catch any arguments that are not explicitly stated
E.g. if input is Int and there are cases for 1 and 2, n can be used to define a case for every other possible argument value which avoids the non-exhaustive error
Where should the bound variable be in the cases?
Last because pattern matching is applied top-down
How are lists constructed in functional languages?
What are the higher-order functions in functional programming?
How does the map function work?
E.g.
Map (+1) [1 .. 10] returns [2 .. 11]
MS for how map function works:
How does the filter function work?
Applies a predicate function to a list of items and returns a list of items that match the given criteria
E.g.
Filter (>3) [1 .. 10] returns [4 .. 10]
How does the fold function work?
Reduces a list to single value by recursively applying a combining function to each element in the list. Combining function takes two arguments and returns a single value.
foldl is fold for exams
E.g.
foldl (+) 0 [1 .. 10] returns 0 + 1 + 2 + .. + 10 = 55
Why are functional programs parallelisable?
Offers several ideal characteristics :
- Immutable data structures
- Statelessness
- Higher-order functions
What are the key characteristics of functional programming and what do they do?
1) Immutable data structures - states or values stored in data structures cannot be changed once created
- Different processing units can apply functions to different items in a list without altering them since they don’t use variables
2) Statelessness - functions do not have side-effects so they do not affect wider state of the programs and outputs only dependent on input
- No need to wait for result of other processes to complete or use results of external processing
3) Higher-order functions - take a function as an a argument and apply to each item
- No need to follow a particular sequence so different processing units can perform at the same time and results can be combined
4) Order of execution determined at runtime - allows instructions to be distribute across different processing units and performed independently
How to combine multiple functions simply in functional programming?
K(g(f(x))) = (k.g.f) x
What is the function signature for FunctionA 5 where FunctionA :: Int -> Int -> Int?
Int -> Int
How to write infix notation in functional programming?
div 4 is the same thingThe add function takes two arguments.
Describe how the add function could be partially applied to the arguments 4 and 6. MS