61A Lecture 4 Friday, September 2 Thursday, September 1, 2011 What Happened with def square(square)? def square(square): return mul(square, square) from operator import mul square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? def square(square): return mul(square, square) from operator import mul square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) 2 Thursday, September 1, 2011 What Happened with def square(square)? def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) Environments & values Expressions 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) Environments & values Expressions square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) Environments & values Expressions square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) Environments & values Expressions square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) square Environments & values Expressions square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) square: 4 square Environments & values Expressions square(4) 2 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) square: 4 square Environments & values Expressions square(4) 3 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) square: 4 square Environments & values Expressions square(4) return mul(square, square) 3 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) square: 4 square Environments & values Expressions square(4) return mul(square, square) 3 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) square: 4 square 16 Environments & values Expressions square(4) return mul(square, square) 3 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) Don't actually name functions and formal parameters the same thing! square: 4 square 16 Environments & values Expressions square(4) return mul(square, square) 3 Thursday, September 1, 2011 What Happened with def square(square)? mul: mul(a,b): def square(square): return mul(square, square) from operator import mul square(4) square: square(square): return mul(square, square) Don't actually name functions and formal parameters the same thing! square: 4 square 16 square(4) return mul(square, square) Environments & values Expressions Environment diagrams will be on holiday until Wednesday 3 Thursday, September 1, 2011 Statements A statement is executed by the interpret to perform an action 4 Thursday, September 1, 2011 Statements A statement is executed by the interpret to perform an action Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... 4 Thursday, September 1, 2011 Statements A statement is executed by the interpret to perform an action Compound statements: Statement <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... 4 Thursday, September 1, 2011 Statements A statement is executed by the interpret to perform an action Compound statements: Statement Clause <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... 4 Thursday, September 1, 2011 Statements A statement is executed by the interpret to perform an action Compound statements: Statement Clause <header>: <statement> Suite <statement> ... <separating header>: <statement> <statement> ... ... 4 Thursday, September 1, 2011 Compound Statements Compound statements: <header>: <statement> Suite <statement> ... <separating header>: <statement> <statement> ... ... 5 Thursday, September 1, 2011 Compound Statements Compound statements: <header>: <statement> Suite <statement> ... <separating header>: <statement> <statement> ... ... A suite is a sequence of statements 5 Thursday, September 1, 2011 Compound Statements Compound statements: <header>: <statement> Suite <statement> ... <separating header>: <statement> <statement> ... ... A suite is a sequence of statements To “execute” a suite means to execute its sequence of statements, in order 5 Thursday, September 1, 2011 Compound Statements Compound statements: <header>: <statement> Suite <statement> ... <separating header>: <statement> <statement> ... ... A suite is a sequence of statements To “execute” a suite means to execute its sequence of statements, in order Execution Rule for a sequence of statements: • Execute the first • Unless directed otherwise, execute the rest 5 Thursday, September 1, 2011 The Fibonacci Sequence 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13 , .. . 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 The Fibonacci Sequence 0, 1, ... pred: curr: 1, 2, 3, 5, 8, 13 , .. . def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 6 Thursday, September 1, 2011 Higher-Order Functions Introduction (Demo) 7 Thursday, September 1, 2011 Pig Introduction (Demo) 8 Thursday, September 1, 2011
© Copyright 2025 Paperzz