Yr1 Functional Programming Flashcards

(30 cards)

1
Q

What is a pure function?

A

Consistent functions that always output the same result for the same input.
They don’t alter external variables or code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is ideal when programming with functions?

A

Only use pure functions.
i.e. functions shouldn’t alter external variables or code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is ideal when programming with functions?

A

Only use pure functions.
i.e. functions shouldn’t alter external variables or code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Wha is immutability?

A

Once a variable is created it shouldn’t be altered. Meaning any functions applied to it should create a new variable for the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What 2 ideas are essential for proper functional programming?

A
  1. Only use pure functions
  2. Variables should be immutable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 4 benefits of functional programming?

A
  1. Code is easier to test as function can be tested individually
  2. Code has fewer bugs due to lack of side effects from functions
  3. Code is easier to understand
  4. Coding is faster
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Haskell?

A

A specific functional programming language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write down the Haskell code for creating a function that doubles a variable x:

A

let double x = x+x
To call:
double 5
Output:
10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a Haskell function for x-y

A

let minus xy = x-y
To call:
minus 10 5
Output:
5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a domain?

A

The input of a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Co Domain?

A

The output of a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How are the domain and co domain of a function displayed in Haskell? Give an example

A

IsEven :: Int -> Bool
Domain is an integer and Co Domain is a Boolean.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a composite function?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a higher order function?

A

HOFs are functions that can take and return given functions as parameters/results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the 3 main higher order functions?

A
  1. Map
  2. Filter
  3. Reduce/fold
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the higher order function map do?

A

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

17
Q

What does the higher order function filter do?

A

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

18
Q

What does the higher order function fold do?

A

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.

19
Q

What does && do in Haskell?

20
Q

What does || do in Haskell?

21
Q

Write the Haskell notation for inverting the Boolean value x

22
Q

Wirte the notation for the 5 useful functions in Haskell?

A
  1. head myList, returns head of the list
  2. tail myList, returns tail of the list
  3. null myList, checks if the list is empty
  4. length myList, returns length of list
  5. 15:myList, prepends 15 to the list
  6. myList++15, appends 15 to the list
23
Q

What is recursion?

A

When a function calls itself to solve a problem step by step.

24
Q

What is the exit clause for a recursive function called?

A

The base case

25
How is a base case defined in Haskell?
factorial 0 =1 Above code defines the base case for below function: factorial x =x*factorial(x-1)
26
What does (x:xs) do when defining a function?
Splits the input list into to variables the head, x, (first item) and the tail, xs, (the remaining items).
27
What is a partial function?
A function that only works for some inputs and may crash or fail for others.
28
Give an example of a partial function:
Head E.g. head [ ] There are no items in the list so the function crashes as it cannot return a head
29
What is a total function?
A function that can handle all possible input and won’t crash.
30
What is partial application?
Hard coding some of the arguments into a function e.g. add4(x) = 4+x