CS 61A Discussion 2 Environments and Higher Order Functions Albert - - PowerPoint PPT Presentation

cs 61a discussion 2
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 2 Environments and Higher Order Functions Albert - - PowerPoint PPT Presentation

CS 61A Discussion 2 Environments and Higher Order Functions Albert Xu Slides: albertxu.xyz/teaching/cs61a/ Announcements functions can return other functions functions can return other functions take in functions as arguments functions can


slide-1
SLIDE 1

CS 61A Discussion 2

Environments and Higher Order Functions

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

functions can return other functions

slide-4
SLIDE 4

functions can return other functions take in functions as arguments

slide-5
SLIDE 5

functions can return other functions take in functions as arguments

slide-6
SLIDE 6

functions can return other functions take in functions as arguments

>>> f = make_power(3) >>> f(5)

slide-7
SLIDE 7

functions can return other functions take in functions as arguments

>>> f = make_power(3) >>> f(5) 125

slide-8
SLIDE 8

functions can return other functions take in functions as arguments

slide-9
SLIDE 9

functions can return other functions take in functions as arguments

>>> apply_twice(square, 3) 81

slide-10
SLIDE 10

Environment Diagrams

let’s look at a function we’ve already seen.

slide-11
SLIDE 11

Environment Diagrams

draw an environment diagram. let’s look at a function we’ve already seen.

slide-12
SLIDE 12

Environment Diagrams

let’s look at a function we’ve already seen. def statement procedure:

1) Write the function on the right side, with appropriate intrinsic name + parent 2) Write the name of the function on the left in current frame 3) Draw an arrow to the function object on the right! 4) Skip the body.

slide-13
SLIDE 13

Environment Diagrams

let’s look at a function we’ve already seen. assignment statement procedure:

1) Evaluate the right hand side 2) Bind that value to a name on the left 3) If the name doesn’t exist in the current frame, add it! Otherwise change the current value

slide-14
SLIDE 14

Environment Diagrams

let’s look at a function we’ve already seen. function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

slide-15
SLIDE 15

Environment Diagrams

let’s look at a function we’ve already seen. function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body *don’t open a frame for built-in functions!

slide-16
SLIDE 16

Environment Diagrams

let’s look at a function we’ve already seen. function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

hi

*don’t open a frame for built-in functions!

slide-17
SLIDE 17

Environment Diagrams

let’s look at a function we’ve already seen. function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

hi None

*don’t open a frame for built-in functions!

slide-18
SLIDE 18

Environment Diagrams

let’s look at a function we’ve already seen. hi None …and we’re done!

slide-19
SLIDE 19

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/