61A Lecture 5 Optional discussion to promote mastery of core - - PDF document

61a lecture 5
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 5 Optional discussion to promote mastery of core - - PDF document

Announcements Quiz 1 scores will be posted eventually, but you already know what you'll get 0/3: Please talk to your TA for advice on how to proceed 1/3: Make sure to spend time understanding all lab & discussion


slide-1
SLIDE 1

61A Lecture 5

Friday, January 30

Announcements

  • Quiz 1 scores will be posted eventually, but you already know what you'll get
§0/3: Please talk to your TA for advice on how to proceed §1/3: Make sure to spend time understanding all lab & discussion questions §2/3: Practice is extremely helpful in learning how to solve CS problems
  • Guerrilla Section 1 on higher-order functions is on Saturday 1/31 in 271 Soda
§Optional discussion to promote mastery of core concepts (prepares you for midterms) §2pm - 4pm is the vanguard section (you commit to helping teach the main section) §4pm - 6pm is the main section §Please do not bring questions about homework or projects to guerrilla sections
  • Small-group tutoring begins next week! Apply online by Sunday if you want a (free) tutor
  • Homework 2 (which is small) is due Monday 2/2 at 11:59pm
  • Project 1 (which is BIG) us due Thursday 2/5 at 11:59pm
  • Midterm 1 on Monday 2/9 7pm-9pm
§Conflict? Fill out the conflict form today! http://goo.gl/2P5fKq 2

Environments for Higher-Order Functions

Environments Enable Higher-Order Functions

(Demo)

4

Environment diagrams describe how higher-order functions work! Functions are first-class: Functions are values in our programming language Higher-order function: A function that takes a function as an argument value or
 A function that returns a function as a return value

Names can be Bound to Functional Arguments

5 Applying a user-defined function:
  • Create a new frame
  • Bind formal parameters


(f & x) to arguments

  • Execute the body:


return f(f(x)) Interactive Diagram

2 1

Environments for Nested Definitions

(Demo)

Environment Diagrams for Nested Def Statements

2 1 3
  • Every user-defined function has

a parent frame (often global)

  • The parent of a function is the

frame in which it was defined

  • Every local frame has a parent

frame (often global)

  • The parent of a frame is the

parent of the function called Nested def

7

Interactive Diagram

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame.
 
 
 Bind <name> to the function value in the current frame When a function is called:

  • 1. Add a local frame, titled with the <name> of the function being called.
  • 2. Copy the parent of the function to the local frame: [parent=<label>]
  • 3. Bind the <formal parameters> to the arguments in the local frame.
  • 4. Execute the body of the function in the environment that starts with the local frame.
8
slide-2
SLIDE 2

Local Names

(Demo)

Local Names are not Visible to Other (Non-Nested) Functions

2 1

“y” is not found “y” is not found, again Error

  • An environment is a sequence of frames.
  • The environment created by calling a top-level function (no def within def)

consists of one local frame, followed by the global frame.

10

Interactive Diagram

Function Composition

(Demo)

The Environment Diagram for Function Composition

2 1 3 1 2 3

Return value of make_adder is an argument to compose1

12

Interactive Diagram

Lambda Expressions

(Demo)

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Lambda expressions are not common in Python, but important in general Important: No "return" keyword! Must be a single expression

14

Lambda expressions in Python cannot contain statements at all!

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the frame in which they were defined.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name.

The Greek letter lambda

15

Currying

slide-3
SLIDE 3

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions Curry: Transform a multi-argument function into a single-argument, higher-order function

17

(Demo)