Functional Programming Flashcards

(25 cards)

1
Q

What do side-effects do?

A

The potential for side-effects means you cannot guarantee that a given function will provide exactly the same output for a given input.

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

What is referential transparency?

A

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.

  • This means that you can more easily determine whether a program will run correctly, leading to fewer bugs and run-time errors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a function?

A

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).

  • This is expessed as f : A -> B where f is the function identifier
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

The subset of values from the co-domain that are mapped to as outputs.

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

What are types?

A

Sets of values (determines what kind of data a value is able to store)

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

What is a function classified as in functional program?

A

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.

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

What is a higher-order function? (MS)

A

A function that takes a function as an argument; and/or returns a function as a result;

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

How to define a function in functional programming?

A

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

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

What is the definition of function appliction?

A

Function application means to apply a function to its argument and can be 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
10
Q

What is the requirements for two functions to be combined?

A
  • f : A -> B
  • g : B -> C
  • g o f : A -> C
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is pattern matching and how is it used in defining functions in functional programming?

A

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)

  • In this case, the function will iterate through each definition until finding one that its argument’s value is suitable for
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a bound variable in functional programming?

A

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

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

Where should the bound variable be in the cases?

A

Last because pattern matching is applied top-down

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

How are lists constructed in functional languages?

A
  • First item in a list is its head
  • Remaining items in the list are its tails
  • Lists are thought of as a concatenation of a head and a tail
  • Represented as (a:as) where a is head and as is tail
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the higher-order functions in functional programming?

A
  • map
  • filter
  • fold
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the map function work?

A
  • Applies a function to a list of items and returns a new list of results
  • Applies function to head of list
  • Recursive call of function on tail of list

E.g.

Map (+1) [1 .. 10] returns [2 .. 11]

MS for how map function works:

17
Q

How does the filter function work?

A

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]

18
Q

How does the fold function work?

A

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

19
Q

Why are functional programs parallelisable?

A
  • Makes it easier to write correct code that can be efficiently distributed across more than one processing unit

Offers several ideal characteristics :
- Immutable data structures
- Statelessness
- Higher-order functions

20
Q

What are the key characteristics of functional programming and what do they do?

A

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

21
Q

How to combine multiple functions simply in functional programming?

A

K(g(f(x))) = (k.g.f) x

22
Q

What is the function signature for FunctionA 5 where FunctionA :: Int -> Int -> Int?

23
Q

How to write infix notation in functional programming?

A
  • div 9 4 is the integer division of the 9 by 4
  • to write infix notation, 9 div 4 is the same thing
  • use backticks
24
Q

The add function takes two arguments.
Describe how the add function could be partially applied to the arguments 4 and 6. MS

25
Figure 14 shows some code written in a functional programming language. total [] = 0 total (x:xs) = x + total (xs) Describe how the total function works to add up all of the numbers in a list. (MS)