Functional Programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a function type?

How would you write this?

A

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

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

What could changing the function type do?

A

The functionality could change without changing the instructions as different input values would be accepted.

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

Explain each part of this f: A –> B

A

F is the function name, A is the domain, B is the co-domain

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

What is the definition of a function in functional programming?

A

A rule that for each element in a set of inputs, assigns an output chosen from another set

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

What is the definition of a domain in functional programming?

A

A domain is a set from which a function’s input values are chosen.

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

What is the definition of a co-domain in functional programming?

A

A co-domain is a set from which the function’s output values are chosen.

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

What are the benefits of the functional programming paradigm?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a first-class object?

A

An object which can appear in an expression, be assigned to a variable, be assigned as an argument and be returned in function calls.

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

Not all combinations of functions are possible. Explain why this is.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a higher order function?

A
  • A function that can take functions as arguments or/and can return a function as a result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the map function do?

A

It applies a function to all the items in a list

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

What does the filter function do?

A

Returns the elements in the domain that satisfy a given condition.

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

What does the fold function do?
What is the difference between foldl and foldr?

Fold is also known as reduce

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Name the three first class objects in functional programming.

A

Functions
Integers
Variables

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

How many arguments can a haskell function take?

A

1, though it can look like 2 arguments are being taken, they are taken as a pair, which is treated as1 argument

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

What is the purpose of partial function application?

A

It allows the processing of multiple arguments by a function.

17
Q

What is meant by the head of a list in functional programming?

A

The first item in the list.

18
Q

What is meant by the tail of a list in functional programming?

A

All the items in the list apart from the first.

19
Q

How many data types can a list in a purely functional language contain?

A

1

20
Q

In functional programming languages, data structures like lists are immutable. What does this mean?

A

The list cannot be changed once created.

21
Q

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?

A

Because f takes two arguments and g only takes one.

22
Q

What is a higher order function?

A

A function which takes a function as an argument, returns a function as a result, or does both.

23
Q

What is an argument?

A

The pieces of data that are passed into a function

24
Q

Give an example of type declaration of a function for multiplying two integers

A

MultiplyUs : : : int –> int –> int

the first two int’s tell us the argument type, the last int tells us the result type

25
Q

Give an example of declaring a function for multiplying two integers

A

MultiplyUs x y = x * y

for example MultiplyUs 3 5 would return 15

26
Q

How are partial function applications implemented?

A

One of the arguments is fixed, leading to a more restricted, specialised function.

27
Q

What is functional composition?

A
  • The process of combining two functions to create a new function
28
Q

What is the benefit of functional composition?

A

User’s cane use the functions both separately, and in conjunction.

29
Q

What is required of functions that are combined?

A

One function’s domain (data type and number of arguments) must be the same as the other functions Co-Domain

30
Q

What do higher order functions use?

A

They use recursion

31
Q

What is function application?

A

The process of giving a function a particular input

32
Q

In functional programming, programs are stateless, what does this mean?

A

That when a program is running, the variables cannot change (e.g. they cannot change state)