What is a pure function?
Consistent functions that always output the same result for the same input.
They don’t alter external variables or code.
What is ideal when programming with functions?
Only use pure functions.
i.e. functions shouldn’t alter external variables or code.
What is ideal when programming with functions?
Only use pure functions.
i.e. functions shouldn’t alter external variables or code.
Wha is immutability?
Once a variable is created it shouldn’t be altered. Meaning any functions applied to it should create a new variable for the result.
What 2 ideas are essential for proper functional programming?
What are the 4 benefits of functional programming?
What is Haskell?
A specific functional programming language.
Write down the Haskell code for creating a function that doubles a variable x:
let double x = x+x
To call:
double 5
Output:
10
Create a Haskell function for x-y
let minus xy = x-y
To call:
minus 10 5
Output:
5
What is a domain?
The input of a function
What is the Co Domain?
The output of a function
How are the domain and co domain of a function displayed in Haskell? Give an example
IsEven :: Int -> Bool
Domain is an integer and Co Domain is a Boolean.
What is a composite function?
A function represented using a dot between two other functions.
The same as doing f(g(x)) but looks like: f.g (use a circle instead of the dot usually
What is a higher order function?
HOFs are functions that can take and return given functions as parameters/results
What are the 3 main higher order functions?
What does the higher order function map do?
Applies the given function to a given list. Notation:
let numbers=[1,2,3,4]
map (double) numbers
The code above doubles all the numbers in the list (double would have to be defined) and returns the list of doubled numbers
What does the higher order function filter do?
Takes a condition and a list and returns a list containing all numbers that met the condition.
E.g.
let numbers = [1,2,3,4]
filter (<16) numbers
What does the higher order function fold do?
Combines all the numbers in a list into one. The type of combination can be specified e.g. * or - or +
foldl (+) 0 numbers
Function above adds all the number in a list and returns the result. Starts with 0.
foldl starts from left and foldr starts from right.
What does && do in Haskell?
And gate
What does || do in Haskell?
Or gate
Write the Haskell notation for inverting the Boolean value x
not(x)
Wirte the notation for the 5 useful functions in Haskell?
What is recursion?
When a function calls itself to solve a problem step by step.
What is the exit clause for a recursive function called?
The base case