CS61A Lecture 18 Amir Kamil UC Berkeley March 4, 2013 - - PowerPoint PPT Presentation

cs61a lecture 18
SMART_READER_LITE
LIVE PREVIEW

CS61A Lecture 18 Amir Kamil UC Berkeley March 4, 2013 - - PowerPoint PPT Presentation

CS61A Lecture 18 Amir Kamil UC Berkeley March 4, 2013 Announcements HW6 due on Thursday Trends project due tomorrow Ants project out Persistent Local State A function with a parent frame The parent contains local state Every call


slide-1
SLIDE 1

CS61A Lecture 18

Amir Kamil UC Berkeley March 4, 2013

slide-2
SLIDE 2

 HW6 due on Thursday  Trends project due tomorrow  Ants project out

Announcements

slide-3
SLIDE 3

Persistent Local State

A function with a parent frame The parent contains local state Every call changes the balance

Example: http://goo.gl/5LZ6F

slide-4
SLIDE 4

Non‐Local Assignment

def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw

Declare the name "balance" nonlocal Re‐bind balance where it was bound previously

slide-5
SLIDE 5

Mutable Values and Persistent State

slide-6
SLIDE 6

Mutable Values and Persistent State

Mutable values can be changed without a nonlocal statement.

slide-7
SLIDE 7

Mutable Values and Persistent State

Mutable values can be changed without a nonlocal statement.

Example: http://goo.gl/cEpmz

slide-8
SLIDE 8

Mutable Values and Persistent State

Mutable values can be changed without a nonlocal statement.

Name‐value binding cannot change

Example: http://goo.gl/cEpmz

slide-9
SLIDE 9

Mutable Values and Persistent State

Mutable values can be changed without a nonlocal statement.

Name‐value binding cannot change Mutable value can change

Example: http://goo.gl/cEpmz

slide-10
SLIDE 10

Creating Two Withdraw Functions

Example: http://goo.gl/glTyB

slide-11
SLIDE 11

Multiple References to a Withdraw Function

Example: http://goo.gl/X2qG9

slide-12
SLIDE 12

The Benefits of Non‐Local Assignment

  

slide-13
SLIDE 13

The Benefits of Non‐Local Assignment

 Ability to maintain some state that is local to a function, but evolves over successive calls to that function.

slide-14
SLIDE 14

The Benefits of Non‐Local Assignment

 Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non‐local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.

slide-15
SLIDE 15

The Benefits of Non‐Local Assignment

 Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non‐local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.  An abstraction of a bank account that manages its own internal state.

slide-16
SLIDE 16

The Benefits of Non‐Local Assignment

 Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non‐local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.  An abstraction of a bank account that manages its own internal state. Weasley Account $10

slide-17
SLIDE 17

The Benefits of Non‐Local Assignment

 Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non‐local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.  An abstraction of a bank account that manages its own internal state. Weasley Account $10 Potter Account $1,000,000

slide-18
SLIDE 18

Referential Transparency

slide-19
SLIDE 19

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

slide-20
SLIDE 20

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3)

slide-21
SLIDE 21

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3)

slide-22
SLIDE 22

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3)

slide-23
SLIDE 23

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3)

Mutation is a side effect (like printing)

slide-24
SLIDE 24

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3)

Mutation is a side effect (like printing) Side effects violate the condition of referential transparency because they do more than just return a value; they change the state of the computer.

slide-25
SLIDE 25

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3)

Mutation is a side effect (like printing) Side effects violate the condition of referential transparency because they do more than just return a value; they change the state of the computer.

slide-26
SLIDE 26

Referential Transparency

Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3)

Mutation is a side effect (like printing) Side effects violate the condition of referential transparency because they do more than just return a value; they change the state of the computer.

slide-27
SLIDE 27

A Mutable Container

slide-28
SLIDE 28

A Mutable Container

def container(contents):

slide-29
SLIDE 29

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions.

slide-30
SLIDE 30

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello')

slide-31
SLIDE 31

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get()

slide-32
SLIDE 32

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello'

slide-33
SLIDE 33

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world')

slide-34
SLIDE 34

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get()

slide-35
SLIDE 35

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world'

slide-36
SLIDE 36

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """

slide-37
SLIDE 37

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get():

slide-38
SLIDE 38

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents

slide-39
SLIDE 39

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value):

slide-40
SLIDE 40

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value): nonlocal contents

slide-41
SLIDE 41

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value): nonlocal contents contents = value

slide-42
SLIDE 42

A Mutable Container

def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value): nonlocal contents contents = value return put, get

slide-43
SLIDE 43

Dispatch Functions

slide-44
SLIDE 44

Dispatch Functions

A technique for packing multiple behaviors into one function

slide-45
SLIDE 45

Dispatch Functions

A technique for packing multiple behaviors into one function

def pair(x, y): """Return a function that behaves like a pair.""" def dispatch(m): if m == 0: return x elif m == 1: return y return dispatch

slide-46
SLIDE 46

Dispatch Functions

A technique for packing multiple behaviors into one function

def pair(x, y): """Return a function that behaves like a pair.""" def dispatch(m): if m == 0: return x elif m == 1: return y return dispatch

Message argument can be anything, but strings are most common

slide-47
SLIDE 47

Dispatch Functions

A technique for packing multiple behaviors into one function

def pair(x, y): """Return a function that behaves like a pair.""" def dispatch(m): if m == 0: return x elif m == 1: return y return dispatch

Message argument can be anything, but strings are most common The body of a dispatch function is always the same: st common

slide-48
SLIDE 48

Dispatch Functions

A technique for packing multiple behaviors into one function

def pair(x, y): """Return a function that behaves like a pair.""" def dispatch(m): if m == 0: return x elif m == 1: return y return dispatch

Message argument can be anything, but strings are most common The body of a dispatch function is always the same:

  • One conditional statement with several clauses
slide-49
SLIDE 49

Dispatch Functions

A technique for packing multiple behaviors into one function

def pair(x, y): """Return a function that behaves like a pair.""" def dispatch(m): if m == 0: return x elif m == 1: return y return dispatch

Message argument can be anything, but strings are most common The body of a dispatch function is always the same:

  • One conditional statement with several clauses
  • Headers perform equality tests on the message
slide-50
SLIDE 50

Message Passing

slide-51
SLIDE 51

Message Passing

An approach to organizing the relationship among different pieces of a program

slide-52
SLIDE 52

Message Passing

An approach to organizing the relationship among different pieces of a program Different objects pass messages to each other

slide-53
SLIDE 53

Message Passing

An approach to organizing the relationship among different pieces of a program Different objects pass messages to each other

  • What is your fourth element?
slide-54
SLIDE 54

Message Passing

An approach to organizing the relationship among different pieces of a program Different objects pass messages to each other

  • What is your fourth element?
  • Change your third element to this new value. (please?)
slide-55
SLIDE 55

Message Passing

An approach to organizing the relationship among different pieces of a program Different objects pass messages to each other

  • What is your fourth element?
  • Change your third element to this new value. (please?)

Encapsulates the behavior of all

  • perations on a piece of data
slide-56
SLIDE 56

Message Passing

An approach to organizing the relationship among different pieces of a program Different objects pass messages to each other

  • What is your fourth element?
  • Change your third element to this new value. (please?)

Encapsulates the behavior of all

  • perations on a piece of data

Important historical role: The message passing approach strongly influenced object‐oriented programming (next lecture)

slide-57
SLIDE 57

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get

slide-58
SLIDE 58

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents):

slide-59
SLIDE 59

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None):

slide-60
SLIDE 60

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents

slide-61
SLIDE 61

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get':

slide-62
SLIDE 62

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get': return contents

slide-63
SLIDE 63

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get': return contents if message == ‘put':

slide-64
SLIDE 64

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get': return contents if message == ‘put': contents = value

slide-65
SLIDE 65

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get': return contents if message == ‘put': contents = value return dispatch

slide-66
SLIDE 66

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get': return contents if message == ‘put': contents = value return dispatch

slide-67
SLIDE 67

Mutable Container with Message Passing

def container(contents): def get(): return contents def put(value): nonlocal contents contents = value return put, get def container_dispatch(contents): def dispatch(message, value=None): nonlocal contents if message == 'get': return contents if message == ‘put': contents = value return dispatch

slide-68
SLIDE 68

Mutable Recursive Lists

slide-69
SLIDE 69

Mutable Recursive Lists

def mutable_rlist():

slide-70
SLIDE 70

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist

slide-71
SLIDE 71

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None):

slide-72
SLIDE 72

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents

slide-73
SLIDE 73

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len':

slide-74
SLIDE 74

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents)

slide-75
SLIDE 75

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem':

slide-76
SLIDE 76

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value)

slide-77
SLIDE 77

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push':

slide-78
SLIDE 78

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents)

slide-79
SLIDE 79

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop':

slide-80
SLIDE 80

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop': item = first(contents)

slide-81
SLIDE 81

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop': item = first(contents) contents = rest(contents)

slide-82
SLIDE 82

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop': item = first(contents) contents = rest(contents) return item

slide-83
SLIDE 83

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop': item = first(contents) contents = rest(contents) return item elif message == 'str':

slide-84
SLIDE 84

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop': item = first(contents) contents = rest(contents) return item elif message == 'str': return str_rlist(contents)

slide-85
SLIDE 85

Mutable Recursive Lists

def mutable_rlist(): contents = empty_rlist def dispatch(message, value=None): nonlocal contents if message == 'len': return len_rlist(contents) elif message == 'getitem': return getitem_rlist(contents, value) elif message == 'push': contents = make_rlist(value, contents) elif message == 'pop': item = first(contents) contents = rest(contents) return item elif message == 'str': return str_rlist(contents) return dispatch