Chapter 1: Building Abstractions with Functions Flashcards
Expressions and Statements
programs consist of instructions to either:
- Compute some value
- Carry out some action
Statements
describe actions
when the interpreter executes a statement it carries out the corresponding action
a statement is executed as it does not return a value it just makes a change in the environment
Expressions
describe computations
when the interpreter evaluates an expression it computes a value of the expression
an expression is evaluated
what is a function?
a function encapsulates logic that manipulates data
what is an object?
an object bundles together data and the logic that manipulates that data, in a way that manages the complexity of both
what is an interpreter?
is a precise procedure that interprets code in a predictable way
errors and debugging
best practices for debugging:
Test Incrementally: well written programs are composed of small, modular components that can be tested individually as you progress. Try executing your components as you go so you can catch errors and be confident in your components
Isolate Errors: an error can be attributed to a particular modular component. Trace the error to the smallest fragment of code you can before trying to correct it
Check your assumptions: since interpreters carry out your instructions to the letter, if there is not a typo, the error is usually in your assumption about the behavior of your code
Consult others: if you don’t understand an error message, ask for help or check a search engine. If you identify an error message but are unsure how to correct it, ask for help.
Themes for success
- Incremental testing
- Modular design
- Precise assumptions
- Teamwork
When describing a language we should pay special attention its means for combining simple ideas to form more complex ideas. Every language has three such mechanisms:
- Primitive expressions and statements: represent the simplest building blocks of a language
- Means of combination: by which compound elements are built from simpler ones
- Means of abstraction: by which compound elements can be named and manipulated as units
what are the two elements the programmers deal with in programming
data and function
Python Libraries
a modules is a collection of functions, whereas a library is a collection of modules
to use the modules from the Python Library, we import them
Names and the environment
a critical aspect of programming is our ability to give name to computational objects that we want to refer to later.
if a value has been given a name we say that the name binds to that value
we establish new bindings using the assignment statement, which uses the assignment operator =
for us to be able to bind a name to a value and later call on that name to access the value, we need to be able to store the name and value binding in memory. This memory is the environment
Environment provide the context for which the evaluation takes place
pure functions vs non-pure functions
a pure function operate as a small machine simply returns a value. The pure function takes in an input and returns the same output for the same input. The property of a pure function is that there are no side effects beyond returning a value.
in short, pure functions cannot have side effects or change over time
a non-pure function generates side effects, which make some change to the state of the interpreter or computer
What are the benefits of pure functions/why do we use pure functions?
- pure functions can be composed more reliably into compound call expressions
- pure functions are simpler to test. Given the same input twice, the pure function will return the same output twice
- pure functions are essential for writing concurrent programs, in which multiple call expressions may be evaluated simultaneously
an environment consists of frames
an environment in which a sequence is evaluated consists of a sequence of frames. Each frame contains bindings. There is a single global frame