Small group tutoring sections (5-6 people), get to see more of the content
I joined a CSM section and found it incredibly useful! Would highly recommend
Discussion videos exist! Walkthroughs for all problems
Results from last discussion
Questions and Comments from last section
The general thing that I enjoy are things completely unrelated to the actual course itself partially because it doesn't take up time that could be done for going over discussion, but also because it's fun
Environment Diagram practice problems
i wonder what this discussion covers
HOFs
i wonder what this discussion covers
Lambda examples and if we have time then currying is quite confusing.
i wonder what this discussion covers
(Formerly) Waitlisted students:
You can get attendance credit for all discussions/labs you missed - just email me if you haven't already for which discussions/labs you've missed
Questions and Comments from last section
Exam prep
Will be at the end of this discussion
When you pass a string into a print statement, what is the type of the object that's printed? It's different from the return statement that displays a string object instead.
It's not actually any type! Think of it as print outputting something that's supposed to be 'human-readable', and return outputting something that's more 'machine-readable'
More on this later in the course
Getting more help at Lab
Just keep your hand raised, we'll get to you
Lab is also collaborative; there is no penalty for looking at other people's code!
Questions and Comments from last section
Other questions
Check the solutions
(for environment diagrams in particular, use tutor.cs61a.org!)
During lab, feel free to collaborate on it! I know the lab room isn't the best for collaboration, but lab is meant to be a space where collaboration is very much allowed!
deff(x):return x
defg(x, y):if x(y):
returnnot y
return y
x = 3
x = g(f, x)
f = g(f, 0)
lambda Functions and Higher-Order Functions
A lambda expression evaluates to a lambda function
Can be used as the operator for a function!
These functions work the same way as a normal function
Can be written in 1 line - faster way to make functions
Similar to def in usage, but different syntax
lambdas are especially useful when you want to use a function once and then never use it again (will see examples of this)
lambda Syntax
lambda <args>: <body>
What goes in <body> must be a single expression
lambda Example
deffunc(x, y):return x + y
func = lambda x, y: x + y
# Notice how I have to do the binding to a variable myself
defi(j, k, l):return j * k * l
i = lambda j, k, l: j * k * l
lambda Example 2
lambda functions can also be used as the operator for a function!
(lambda x, y: x + y)(2, 3) # 5# Equivalent todefadd(x, y):return x + y
add(2, 3) # 5
Higher Order Functions (HOF)
HOFs are functions that can do the following things (can be both):
Take in other functions as inputs
Return a function as an output
You can treat a function as just an object or a value (there's nothing special about them)
function and function() mean different things!
function refers to the object itself (in the environment diagram, it refers to what the arrow is pointing to)
function() actually calls and executes the body of the function
HOF Example 1 (Functions as input)
defdouble(x):return x * 2defsquare(x):return x ** 2defdouble_adder(f, x):return f(x) + f(x)
double_adder(double, 3) # 12
double_adder(square, 3) # 18# Passed in two different functions
HOF Example 2 (Functions as output)
deff(x):defg(y):return x + y
return g
a = f(2)
a(3) # 5# Same thing as calling f(2)(3)
HOF Example 2
deff(x):defg(y):defh(z):return x + y + z
return h
return g
lambda x: lambda y: lambda z: x + y + z
The two above are equivalent statements!
(Notice how the lambda one takes up far less space!)
Worksheet!
Currying
Currying is one application of the HOFs from earlier.
lambda x: lambda y: x + y
Instead of just any expression on the inside (for example x + y), we use a function!
defpow(x, y):
x ** y
defcurried_pow(x):deff(y):returnpow(x, y)
return f
curried_pow(3)(2)
# is the same aspow(3, 2)
# You will need as many inner functions as you have arguments
Currying
Currying is the process of turning a function that takes in multiple arguments to one that takes in one argument.
What's the point?
Sometimes functions with 1 argument are far easier to deal with
Can create a bunch of functions that have slightly different starting values which saves on repeating code
Useful for the map function (it requires functions that have only 1 argument)
Kind of hard to see the benefits until you write production code
Worksheet!
Mental Health Resources
CAPS:
If you need to talk to a professional, please call CAPS at 510-642-9494.
After Hours Assistance
For any assistance after hours, details on what to do can be found at this link