Functional Programming Flashcards

(25 cards)

1
Q

What is a function?

A

Rule that maps each element form a domain to a co-domain

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

What is a side-effect?

A

A change to the content / state around the function

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

What are the consequences of side effects?

A

Means that you cannot guarantee that a given function will provide exactly the same output form a given input

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

What is the image of a function?

A

Subset of values from the co-domain that are mapped as outputs

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

What are first-class objects?

A

Objects that:
- Can appear in expressions
- Can be assigned to variables
- Can be passed as an argument to another function
- Can be returned as a result of a function

Functions are first-class objects

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

What is function application?

A

The process of applying a function to its argument // described as the process of giving particular inputs to a function

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

How do you define a function in Haskell?

A

Declare the function’s type using a function signature and state the expressions that map inputs to outputs

Double :: Int -> Int
Double x = x * 2

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

What is a function’s type?

A

The set of values that form the functions domain and co-domain. If f:A->B is the function then A->B is the type

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

What is the function type of f : A -> B -> C

A

Type is A x B -> C where A x B is the cartesian product of the sets A and B.

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

What does f . g mean?

A

A new function composed of f and g where g is evaluated before f

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

When can two functions be composed?

A

If the codomain of the first matches the domain of the other

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

What is a partially applied function?

A

A function that requires multiple parameters but not all have been provided

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

Explain how the function add 4 5 is evaluated using partial function application

A
  1. The add function isn applied to the first argument, 4
  2. This outputs a new function that takes a single argument, add4
  3. This function add the value of its argument to the value of the first argument, add4 5 adds 4 and 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What must one pattern matching expression contain in order for no errors to occur?

A

A bound variable that ensures that the function has a mapping for every possible value in the domain

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

What is a list in functional programming?

A

Concatenation of a head and tail where the head is an element and a tail is another list

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

How do you return the head of the following list in Haskell: L = [1,2,3,4]

17
Q

How do you return the tail of the following list in Haskell: L = [1,2,3,4]

18
Q

What does the map function do?

A

A function that applies a function to a list of items, returning a new list of results

19
Q

What does the filter function do?

A

Applies a predicate function to a list of values, returning a new list of items that match the given criteria

20
Q

What does the fold/reduce function do?

A

Reduces a list to a single value by recursively applying a combining function to each element in the list. A starting value for the combining function must be provided

21
Q

What is a combining function?

A

A function that takes two arguments and returns a single value.

22
Q

Why are functional programs paralelisable?

A
  • Immutable data structures
  • Statelessness
  • Using higher-order functions
  • Order of operation of a functional programs can be determined at run-time
23
Q

Describe how immutable data structures make programs parallelisable

A
  • States or values stored in data structures cannot be changed
  • Different processing units can apply functions to different items in a list without worrying that they have been externally altered
24
Q

Describe how statelessness can make programs parellisable

A
  • Functions do not have side-effects, they do not affect a global state
  • No need to wait for the result of another process to complete
25
Describe how high-order functions make programs parelisable
- No requirement that functions follow any particular sequence, and so function application can be performed by different processes and the results combined at the end