Module 1: Functional programming Flashcards

Intro, Environment, Pure Functions, Higher-order Functions

1
Q

What is the imperative programming paradigm?

A

The code contains commands that specify step-by-step changes of the internal state or environment of the program

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

What is a “state”?

A

What we keep in memory i.e. variables and their values

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

What does it mean to
change the “state”?

A

Add, modify or remove variables

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

What kind of language was Python designed to be?

A

Imperative programming language

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

Give me examples of imperative code.

A

a=3 #After executing this command, the internal state has an
#additional variable a with the value 3.

a=a+4 #after executing this command, the internal state has
#changed: the variable a has a different value.

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

What does the imperative paradigm focus on?

A

The imperative paradigm focuses on how to arrive at solutions.

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

How do we use the imperative paradigm?

A
  1. We specify the sequences of instructions that will produce the desired output
  2. These instruction change the state of the computation by adding, modifying and removing information
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is another name for imperative paradigm?

A

Procedural

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

Can imperative programming be combined with other paradigms, and if yes give an example.

A

Yes, it can be combined with compatible paradigms
e.g. Object Oriented Paradigm

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

What are other approaches to programming (then imperative)?

A
  • Declarative programming
  • Functional programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is declarative programming and give an example.

A

Declarative programming describes the environment with rules and facts.
e.g. Prolog

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

factorial(N,F) :-

N>0,

N1 is N-1,
factorial(N1,F1),

F is N * F1.
?- factorial (3, W).
W = 6

What programming language is this code exemplifying and explain what’s going on.

A

Declarative programming.

Describes the environment:
factorial(N,F) :-

N>0,

N1 is N-1,
factorial(N1,F1),

F is N * F1. #F and F1 return variables

Specifies the goal:
?- factorial (3, W).

The interpreter derives the necessary computations to reach the goal given the rules and facts. The programmer does not need to write the instructions to arrive at the goal.
W = 6

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

Does the programmer need to write instructions to arrive at the goal in declarative programming?

A

No.

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

What is the subtype of declarative programming?

A

Functional programming

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

What does functional programming emphasize?

A

It puts emphasis on FUNCTIONS, particularly:

  • Design principles to create functions without side effects
  • Use of function compositions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What should functions do to avoid side effects

A
  • Restricting the use of global variables
  • Defining functions such that their only observable output is the return value
  • Defining functions such that their output only depends on the input arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Where do variables defined in Python live?

A

Environment/Namespace

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

What is a namespace?

A

A namespace is a dictionary that maps identifiers (variable or function names) to objects

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

Give examples of identifiers.

A
  • Variables
  • Function names
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Is everything in Python an object?

A

Yes. Even functions!

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

How many namespaces are present in Python?

A

3

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

What namespaces does Python have?

A
  • Local
  • Global
  • Built-in
22
Q

What is Functional Programming aiming to do?

A

Functional Programming aims to minimize the dependence from the state of the environment

23
Q

How does Functional programming minimise its dependence on the state of the environment?

A
  1. Functions should produce an output that depends solely on the input (arguments) (they should not rely on the current state of the outer environment (globals)
  2. Functions should not change the environment outside the function
24
Q

How does Functional programming not change the environment outside the function?

A
  1. Global arguments should not be modified
  2. Local variables can be created and modified
  3. Arguments should remain unchanged once the function has finished
25
Q

What does Functional programming do to reduce side-effects?

A

Restrict or forbid state changes of the environment.

26
Q

Why is restricting/forbidding state changes of the environment useful?

A

The computation is predictable: functions don’t depend on global values, and don’t change them. They only depend on their input.

27
Q

What are purely functional languages and give an example?

A

Languages that are strict in forbidding state changes
e.g. Haskell

28
Q

Is Python purely functional? Explain.

A

Python is NOT a purely functional programming language (not even functional).
Python doesn’t forbid changes and was designed to be an imperative language.
Python is used as a multi paradigm language

29
Q

How does python support functional programming?

A

Through built-in functions and features

30
Q

What is the core of functional programming?

A

The core of functional programming is the use of pure functions.

31
Q

What are pure functions?

A

Pure functions always return the same results when the same arguments are given to it.

32
Q

What are the characteristics of pure functions?

A
  • Pure function DO NOT depend on or modify the state of the environment
  • Operate ONLY locally
  • Avoid global statements
33
Q

How do pure functions avoid side-effects?

A

Pure function avoid side-effects by being independent of the state of the environment

34
Q

How can we write pure functions?

A

Through mathematical expressions
e.g. Lambda functions

35
Q

What is a lambda function and what is the basic structure?

A

Lambda functions are non-linear anonymous functions, restricted to a single expression

lambda x:x**2

36
Q

What do lambda functions do?

A
  • Define an expression
  • Can be evaluated to a value
37
Q

Can lambda function create or modify variables? Why or why not.

A

Lambda function CANNOT create or modify any variables.
THe same input always produces the same output

38
Q

What are higher-order functions?

A
  • Accept a function as an argument

OR

  • Return a function as a value
39
Q

Why are higher-order functions necessary in Functional programming?

A

Higher-order functions are needed because the program is created by composing (pure) functions.

40
Q

What are first-class functions?

A

Functions that can be treated as first-class citizens in a programming language. This means they can be assigned to variables, passed as arguments to other functions, and returned from other functions just like any other value.

41
Q

What is lambda used for and give an example?

A

Lambda functions are used to write higher-order functions.

42
Q

Provide a specific example of a higher-order function.

A

EXAMPLE:
year_cheese = [(2001,30.12), (2002, 30.6),…)

The built-in function max compares tuples starting from position 0:
»>max(year_cheese)
(2010, 32.92)

To modify it, we provide a function as an argument to max:
»> max(year_cheese, key = lambda yc: yc [1])
(2007, 33.5)

This is a higher order function: the function max receives a lambda function as an argument

43
Q

What are higher-order functions in python?

A
  • lambda
  • map()
  • filter()
  • min()
  • max()
  • sorted()
44
Q

What does the map function do and give an example

A
  • map(f, iterables)

Returns an iterator that computes f over arguments of each iterable.

  • Example:
    “” . join(map(str.upper, “Here is my iterable”))> > > ‘HERE IS MY ITERABLE’
45
Q

What does the filter function do and give an example.

A

Returns an iterator with the items in iterables for which f is True

46
Q

What does the reduce function do and give an example

A
  • The reduce function is not built-in (though it was originally!).
  • It’s a higher-order function, and it applies another function to each pair of items in an iterable, accumulating each partial result.
  • This technique is known as reduction or folding
47
Q

What does this piece of code do?
reduce(lambda x, y: x+y, d)

A

Given a list such as [2,4,5,6,9], reduce applies the lambda function to each pair (((2+4)+5)+6)+9), and returns the cumulative value (in this case, 26)

48
Q

What does this piece of code do?
““.join(filter(str.isupper, “Here is my iterable”))

A

Output: “H”
Returns an iterator with the items in iterables for which f is True

49
Q

What is the general form of a map-reduce algorithms? Explain its parts.

A

reduce(f, map(g, data))
- The function g performs an item-by-item mapping.
- The function f handles the reduction or folding.
- This is a way of composing functions!

50
Q

Why are higher order functions useful?

A
  • They can simplify complex loops
  • They can be chained (function composition!)
  • They are very general: many computations can be expressed as reductions
51
Q

What is more abstract: Functional programming or Imperative programming?

A

Functional programming.

52
Q

What are properties of Functional programming?

A
  • They can simplify complex loops
  • They can be chained (function composition!)
  • They are very general: many computations can be expressed as reductions