4.12 Fundamentals of Functional Programming Flashcards

1
Q

Main Features of Functional Programming (6)

A
  • Functions used as building blocks
  • Statements written as a series of functions
  • Each accepts input data as arguments and returns an output
  • Statelessness and immutability
    • Can’t overwrite/edit
  • All can do is perform cals + return a value
  • Inputs/outputs can be values or functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Benefits of Functional Programming (4)

A
  • Easy to write, understand, predict behaviour
  • Less prone to bugs as no overwriting/editing
  • Allows parallel processing
    • Data can’t be altered by previous function so no worries about execution order
    • More than 1 processor can work on different parts of large data set at once
    • Won’t affect any other part of data
  • Helps deal with big data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A First Class Object is one that may (4)

A
  • Be assigned to a variable
  • Appear in an expression
  • Be assigned to an argument
  • Be returned in a function call
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Function Application (2, Example)

A
  • Process of giving particular inputs to a function
  • Apply/call function by using name followed by argos needed separated by spaces

addThreeInputs(x, y, z) = x + y + z
Integer → Integer → Integer → Integer ( x → y → z → output)

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

Partial Function Application (3, Example)

A
  • Uses a function + applies it partially with another argument
  • Reduces num of args passed to function
  • Can perform multiple times to further reduce

E.g. Area(A,B) → AB
Partially apply Area to the arguments 3 & 5:
Area5(A) → A
5 (only need to apply 1 argument)

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

Composition of Functions (1, Example)

A
  • Process results in combining two functions to get a new function

E.g. f: A → B f(x) = x + 2
g: B → C g(x) = x2
Function: g𐩑f = (x+2)2
Domain = A, Co-Domain = C

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

Function (definition)

A

A rule that for each element of some set A of inputs, assigns an output from a chosen set B but without necessarily using every member of set B

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

Domain and Co-Domain (definition) (1)

A
  • Domain = set of inputs
  • Co-Domain = set of outputs
  • Can have different set/data types of domain & co domains
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Higher Order Functions (definition)

A
  • Takes a function as an arg or returns a function as a result or both
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

MAP (def, recursion process, example)

A
  • Applies given function to each element in list and returns a list
    Recursive: take Head of list, apply function, recursive through Tail
  • E.g. Square(x) = x2
  • Map Square([4, 7, 1, 2]) Outputs: [16, 49, 1, 4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

FILTER (def, example)

A
  • Processes a data structure to produce new structure containing exactly elements of original that match a given condition
  • E.g. Filter(>10): [8. 9, 10, 11, 12] Outputs: [11, 12]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

FOLD OR REDUCE (def, example)

A
  • Reduces list of values to single value by repeatedly applying a combining function to list
  • E.g. Fold 1 (+) [4, 7, 9, 1] Outputs: 1+4+7+9+1: 22
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Head and Tails (4)

A
  • Head: Tail
  • The Head is always a value
  • The Tail is always a list
  • Can use head and tail to write functions that use recursion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly