Suggested Homework #6 (for Quiz #6 on 11/29/07)

Problem 1:
(a) What is the difference between assignment and mutation ?
(b) What are three differences between ML's environment and Python's namespace ?
(c) In each of the following pieces of Python code, indicate by circling the appropriate parts, where in the code assignment is taking place and where mutation is taking place.
(i)
s = range(10)
for i in s:
  s[i] = s[i] + 1
  i = i + 1
(ii)
s = [[x] for x in range(10)]
for [i] in s:
  s = s + s[i]
(iii)
s = [[x] for x in range(10)]
for [i] in s:
  s[i] = s + s[i]
Problem 2:
(a) When are variables x1,x2,...,xn said to be aliases ?
(b) For each of the following, which variables are aliased after the code is entered into Python ? (i)
x = ["a","b","c"]
y = [1,2,3,4,x]
z = y[4]

(ii)
y = ["a","b","c"]
z = [1,2,3]

def f(x):
  x = z

f(y)

(iii)
x = [1,2,3]
z = 10

def f(y):
  x[0] = y
  x[1] = x[0]

f1 = f
f1(z)
w = x[1]

Problem 3:
What is the value of z at the end of the following program:
x=[0]
def f(y):
  def g():
    x[0] = x[0] + 1
    y[0] = y[0] + 1
    return(y[0],x[0])
  return g

g1 = f([10])
g2 = f([100])
z = [g1(),g1(),g1(),g1(),g2(),g2(),g2(),g2()]