Chapter 1: Building Abstractions with Functions Flashcards

1
Q

Expressions and Statements

A

programs consist of instructions to either:

  1. Compute some value
  2. Carry out some action
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Statements

A

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

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

Expressions

A

describe computations

when the interpreter evaluates an expression it computes a value of the expression

an expression is evaluated

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

what is a function?

A

a function encapsulates logic that manipulates data

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

what is an object?

A

an object bundles together data and the logic that manipulates that data, in a way that manages the complexity of both

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

what is an interpreter?

A

is a precise procedure that interprets code in a predictable way

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

errors and debugging

A

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.

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

Themes for success

A
  1. Incremental testing
  2. Modular design
  3. Precise assumptions
  4. Teamwork
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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:

A
  1. Primitive expressions and statements: represent the simplest building blocks of a language
  2. Means of combination: by which compound elements are built from simpler ones
  3. Means of abstraction: by which compound elements can be named and manipulated as units
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what are the two elements the programmers deal with in programming

A

data and function

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

Python Libraries

A

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

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

Names and the environment

A

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

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

pure functions vs non-pure functions

A

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

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

What are the benefits of pure functions/why do we use pure functions?

A
  1. pure functions can be composed more reliably into compound call expressions
  2. pure functions are simpler to test. Given the same input twice, the pure function will return the same output twice
  3. pure functions are essential for writing concurrent programs, in which multiple call expressions may be evaluated simultaneously
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

an environment consists of frames

A

an environment in which a sequence is evaluated consists of a sequence of frames. Each frame contains bindings. There is a single global frame

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

intrinsic name vs bound name

A

different names may refer to the same function, but that function itself has only one intrinsic name

17
Q

What is a function signature?

A

a description of the formal parameters of a function

18
Q

what is a compound expression?

A

a combination of other expressions

19
Q

what is a call expression?

A

the most important kind of compound express, which applies a function to some arguments

20
Q

example of a compound expression

A

an expression representing a number combined with mathematical operators forms a compound expression

21
Q

the three core attributes of a functional abstraction which is critical to mastering the use functional abstraction are

A
  1. Domain: the set of arguments it can take
  2. Range: the set of values it can return
  3. Intent: is the relationship it computes between inputs and output (including side effects)

example of the core attributes for any square function

  1. domain: any single real number
  2. range: any non-negative real number
  3. intent: the output is the square of the input

note: the attributes do not explain the detail of how the intent is carried out. That detail is abstracted away

22
Q

How to design good functions

A
  1. Each function should have exactly one job. That job should be identifiable with a short_name and be able to be clearly described with a single line of text. Functions that perform multiple jobs in sequence should be divided into multiple functions
  2. DRY - do not repeat yourself. If you find yourself copying and pasting a block of code or rewriting logic… this is likely an opportunity for functional abstraction
  3. Functions should be defined generally

these guidelines improve the readability of the code, reduce the number of errors, and minimize the total amount of code written

Decomposing a complex task into concise functions is a skill that takes experience to master

23
Q

What is Documentation and how to implement it in your code

A

Documentation is the practice of including a description of a function in the function definition, called a docstring. It must be indented along with the function body and enclosed with triple quotes

the first line describes the job of the function in one line

the following lines describe arguments and clarify behavior of the function

you can access the docstring of a function when you call help with the function name as the argument

24
Q

how to implement comments

A

add comments at the end of a line

the syntax is simply beginning the comment with a # symbol