Lecture 02 Names & Functions
These are my notes for SICP(Structure and Interpretation of Computer Programs). Hope they’ll be of some help to you.
Names
Description
-
Values can be assigned to names to make referring to them easier.
-
A name can only be bound to a single value.
-
One way to introduce a new name in a program is with an assignment statement.
Statement
Statements affect the program, but do not evaluate to values.
Assignment
x = 1 + 2*3 - 4//5
- Names are bound to values in an environment
To execute an assignment statement:
- Evaluate the expression to the right of =.
- Bind the value of the expression to the name to the left of = in the current environment.
Functions
Description
-
Functions allow us to abstract away entire expressions and sequences of computation
-
They take in some input (known as their arguments) and transform it into an output (the return value)
-
We can create functions using def statements. Their input is given in a function call, and their output is given by a return statement.
Defining functions
from math import pi
def volum(r): #def <name>(<parameters>): -Function signature
return (4/3) * pi * (r**3) #return <return expression> -Function body
- Function signature indicates name and number of arguments
- Function body defines the computation performed when the function is applied
- Def statements are a type of assignment that bind names to function values
Procedure for calling/applying user-defined functions (for now)
-
Create a new environment frame (we call it local frame, included in global frame)
-
Bind the function’s parameters(formal arguments) to its arguments(actual arguments) in that frame
-
Execute the body of the function in the new environment
from operator import mul #built-in function
def square(x):
return mul(x, x) #user-defined function
y = square(-2)
Some built-in functions
from math import pi #pi = Π = 3.1415926......
from math import e #e = 2.71828......
from math import exp #exp(x) = e ^ x
from math import expm1 #expm1(x) = e ^ x - 1
from math import log #log(x,y) = lnx/lny; log(x) = lnx
from math import log10(x) #log10(x) = lgx
from math import log1p(x) #log1p(x) = ln(1+x)
from math import ceil #ceil(x) = [x] + 1
from math import floor #floor(x) = [x]
from math import pow #pow(x, y) = x ^ y
from math import sqrt #sqrt(x) = the square root of x
from math import fabs #fabs(x) return the absolute of x and the return number is a float
from math import factorial #factorial(x) = x!
from math import hypot #hypot(x, y) = sqrt(x*x + y*y)
from math import sin
from math import asin
from math import cos
from math import acos
from math import tan
from math import atan
abs(x) #abs(x) = |x| even if x is complex
complex(x, y) #create a complex with x to be real and y to be imaginative; y is not necessary
pow(x, y) #pow(x, y) = x ^ y
round(x, y) #round x to y decimal places, you don't have to input y if y = 0
input([prompt])
print(...)
Summary
-
Programs consist of statements, or instructions for the computer, containing expressions, which describe computation and evaluate to values.
-
Values can be assigned to names to avoid repeating computations.
-
An assignment statement assigns the value of an expression to a name in the current environment.
-
Functions encapsulate a series of statements that maps arguments to a return value.
-
A def statement creates a function object with certain parameters and a body and binds it to a name in the current environment.
-
A call expression applies the value of its operator, a function, to the value(s) of its operand(s), some arguments.