Module 1: Functional programming Flashcards
Intro, Environment, Pure Functions, Higher-order Functions
What is the imperative programming paradigm?
The code contains commands that specify step-by-step changes of the internal state or environment of the program
What is a “state”?
What we keep in memory i.e. variables and their values
What does it mean to
change the “state”?
Add, modify or remove variables
What kind of language was Python designed to be?
Imperative programming language
Give me examples of imperative code.
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.
What does the imperative paradigm focus on?
The imperative paradigm focuses on how to arrive at solutions.
How do we use the imperative paradigm?
- We specify the sequences of instructions that will produce the desired output
- These instruction change the state of the computation by adding, modifying and removing information
What is another name for imperative paradigm?
Procedural
Can imperative programming be combined with other paradigms, and if yes give an example.
Yes, it can be combined with compatible paradigms
e.g. Object Oriented Paradigm
What are other approaches to programming (then imperative)?
- Declarative programming
- Functional programming
What is declarative programming and give an example.
Declarative programming describes the environment with rules and facts.
e.g. Prolog
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.
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
Does the programmer need to write instructions to arrive at the goal in declarative programming?
No.
What is the subtype of declarative programming?
Functional programming
What does functional programming emphasize?
It puts emphasis on FUNCTIONS, particularly:
- Design principles to create functions without side effects
- Use of function compositions
What should functions do to avoid side effects
- 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
Where do variables defined in Python live?
Environment/Namespace
What is a namespace?
A namespace is a dictionary that maps identifiers (variable or function names) to objects
Give examples of identifiers.
- Variables
- Function names
Is everything in Python an object?
Yes. Even functions!
How many namespaces are present in Python?
3