Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 1 of 40
Teaching Strategies for ReinforcingStructural Recursion with Lists
Michael H. Goldwasser David Letscher
Saint Louis University
Active Learning
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 2 of 40
I need somevolunteersfor today
Structural Recursion
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 3 of 40
For an object-oriented CS1, structural recursion canbe more natural than functional recursion.
An object is composed of a basic shape and a(recursive) instance of the same class.
Beyond Graphics
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 4 of 40
Pro: Graphics are fun and tangible.
Beyond Graphics
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 4 of 40
Pro: Graphics are fun and tangible.
Con: recursive patterns are generally limited(“draw outer, draw rest”; “move outer, move rest”)
Beyond Graphics
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 4 of 40
Pro: Graphics are fun and tangible.
Con: recursive patterns are generally limited(“draw outer, draw rest”; “move outer, move rest”)
Our goal is to provide a tangible presentation for anon-graphical example of structural recursion(namelypurely-recursive lists).
Python’s List Class
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 5 of 40
Python supports alist class as a standard container.
Python’s List Class
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 5 of 40
Python supports alist class as a standard container.
Disclaimer:the internal implementation is not trulyrecursive; its an expandible array akin to Java’sArrayList or C++’svector.
Python’s List Class
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 5 of 40
Python supports alist class as a standard container.
Disclaimer:the internal implementation is not trulyrecursive; its an expandible array akin to Java’sArrayList or C++’svector.
Public Interface:Our students are very familiarwith use of this class and its menu of behaviors(we use lists from the opening weeks of CS1).
Python’s List Class
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 5 of 40
Python supports alist class as a standard container.
Disclaimer:the internal implementation is not trulyrecursive; its an expandible array akin to Java’sArrayList or C++’svector.
Public Interface:Our students are very familiarwith use of this class and its menu of behaviors(we use lists from the opening weeks of CS1).
This allows us todecoupletwo potentiallyintertwined concepts:
1. the use of recursion2. the abstraction of a container class
Emulating Python’s List Class
Overview
Active Learning
Structural Recursion
Beyond Graphics
Python’s List Class
Role Playing
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 6 of 40
We rely on the familiar public interface byprecisely emulatingPython’slist class, includingbehaviors such as:
count(value) len ( )index(value) contains (value)append(value) getitem (index)insert(index, value) setitem (index, value)remove(value) repr ( )
This allows us to sidestep the design issue ofparameterizing the recursion.
Role Playing
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 7 of 40
Role Playing
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 8 of 40
Classic activity for teaching object orientation.
Role Playing
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 8 of 40
Classic activity for teaching object orientation.
Classic activity for teaching (functional) recursion.
Role Playing
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 8 of 40
Classic activity for teaching object orientation.
Classic activity for teaching (functional) recursion.
Limited history for the combination of these ideas.
Ground Rules for Students
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 9 of 40
OurList Class: an instance will be representedrecursively using two attributes:
■ head: a reference to the first element (if any)
■ rest: a reference to a secondary list with allremaining elements (if any)
Our base case is anempty list, represented withboth head and rest set to theNone reference.
An empty list is a natural concept for our studentsbecause Python’s default list instance is empty.
State Information
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 10 of 40
Each actor is given a slip of paper that representshis/her state information.
head:rest:
: OurList
State Information
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 10 of 40
Each actor is given a slip of paper that representshis/her state information.
head:rest:
: OurList
Per'E'Sharon
Example: here is the slip currently held by Sharon .
State Information
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 10 of 40
Each actor is given a slip of paper that representshis/her state information.
head:rest:
: OurList
NoneNone
Matthew
Example: here is the slip currently held byMatthew .
Message Passing
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 11 of 40
We enforce strict “message passing” for allcommunication.
Activation records are sent inside a tennis ball.
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
Message Passing
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 11 of 40
We enforce strict “message passing” for allcommunication.
Activation records are sent inside a tennis ball.
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Errol
Michael
Let’s get started with a callErrol .count('E')
Errol ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 12 of 40
head:rest:
: OurList
Sharon
'F'Errol ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Errol
Michael
Errol ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 12 of 40
head:rest:
: OurList
Sharon
'F'Errol ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Errol
Michael
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Sharon
Errol
Sharon ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 13 of 40
head:rest:
: OurList
Per
'E'Sharon ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Sharon
Errol
Sharon ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 13 of 40
head:rest:
: OurList
Per
'E'Sharon ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Sharon
Errol
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Per
Sharon
Sharon ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 13 of 40
head:rest:
: OurList
Per
'E'Sharon ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Sharon
Errol
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'
1
Per
Sharon
Sharon ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 13 of 40
head:rest:
: OurList
Per
'E'Sharon ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'2
Sharon
Errol
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'
1
Per
Sharon
Errol ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 14 of 40
head:rest:
: OurList
Sharon
'F'Errol ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'Errol
Michael
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'
2
Sharon
Errol
Errol ’s Point of View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 14 of 40
head:rest:
: OurList
Sharon
'F'Errol ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'2
Errol
Michael
ACTIVATION RECORD
Sent to:Method:Parameters (if any):Please return to:
Return Value (if any):
count'E'
2
Sharon
Errol
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
count('E')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
count('E')
0
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
count('E')
10
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
count('E')
11
0
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
count('E')
21
10
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Sequence Diagram
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 15 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
count('E')count('E')
count('E')count('E')
count('E')
22
11
0
head:rest:
: OurList
Per
'E'Sharon
head:rest:
Local View
Overview
Role Playing
Role Playing
Ground Rules
State Information
Message Passing
Point of View
Sequence Diagram
Local View
Variants
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 16 of 40
rest:
: OurList
Per
'E'Sharon
count('E')count('E')
21
head:
Variants
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 17 of 40
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 18 of 40
head:rest:
: OurList
Sharon
'F'Errol
index('T')
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 18 of 40
head:rest:
: OurList
Sharon
'F'Errol
index('T')index('T')
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 18 of 40
head:rest:
: OurList
Sharon
'F'Errol
index('T')
1
index('T')
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 18 of 40
head:rest:
: OurList
Sharon
'F'Errol
index('T')
1
index('T')
2
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 19 of 40
rest:
: OurList
Dale
'T'Per
index('T')
head:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 19 of 40
rest:
: OurList
Dale
'T'Per
index('T')
0
head:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')index('T')
index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')
0
index('T')index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')
0
index('T')
1
index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')
0
index('T')
12
index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The index method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 20 of 40
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Sharon
'F'Errol
index('T')
0
index('T')
12
index('T')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
This differs fromcount because the recursion doesnot necessarily proceed to an empty list.
The getitem method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 21 of 40
In Python, the operator syntaxdata[2] isimplemented with a call todata. getitem (2).
head:rest:
: OurList
Sharon
'F'Errol
getitem (2)
The getitem method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 21 of 40
In Python, the operator syntaxdata[2] isimplemented with a call todata. getitem (2).
head:rest:
: OurList
Sharon
'F'Errol
getitem (1)getitem (2)
The getitem method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 21 of 40
In Python, the operator syntaxdata[2] isimplemented with a call todata. getitem (2).
head:rest:
: OurList
Sharon
'F'Errol
getitem (1)
'T'getitem (2)
The getitem method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 21 of 40
In Python, the operator syntaxdata[2] isimplemented with a call todata. getitem (2).
head:rest:
: OurList
Sharon
'F'Errol
getitem (1)
'T'getitem (2)
'T'
The getitem method
Overview
Role Playing
Variants
The index methodThe getitemmethod
Recursive Patterns
Mutators
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 21 of 40
In Python, the operator syntaxdata[2] isimplemented with a call todata. getitem (2).
head:rest:
: OurList
Sharon
'F'Errol
getitem (1)
'T'getitem (2)
'T'Note: the parameter value changes during therecursion; the return value does not change.
Recursive Patterns
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 22 of 40
base case parameters return valuemethod empty head index same vary none same vary none
len ✓ ✓ ✓contains ✓ ✓ ✓ ✓getitem ✓ ✓ ✓ ✓setitem ✓ ✓ ✓ ✓
repr ✓ ✓ ✓count ✓ ✓ ✓index ✓ ✓ ✓ ✓
append ✓ ✓ ✓insert ✓ ✓ ✓ ✓
remove ✓ ✓ ✓ ✓
Mutators
Overview
Role Playing
Variants
Mutators
Mutators
Theappend method
insert, remove, pop
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 23 of 40
Easiest: setitem
It is a one-for-one change of data,without any structural change on the list.
(very similar pattern to getitem )
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')
'S'head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
: OurList
NoneNone
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')
'S'OurList( )
head:rest:
: OurList
Per
'E'Sharon
head:rest:
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
Judy
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
Judy
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
Judy
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
Judy
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
Judy
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
The append method
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 24 of 40
None
Matthew
head:rest:
: OurList
Dale
'T'Per
head:rest:
: OurList
Matthew
'E'Dale
head:rest:
: OurList
NoneNone
Judy
head:rest:
: OurList
Sharon
'F'Errol
append('S')append('S')
append('S')append('S')
append('S')OurList( )
Judy
'S'
Judy
head:rest:
: OurList
Per
'E'Sharon
head:rest:
: OurList
None
Instructor can highlight the system’s memory management.
insert, remove, pop
Overview
Role Playing
Variants
Mutators
Mutators
Theappend method
insert, remove, pop
Implementation
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 25 of 40
Arbitrary insertions and deletions can be performed(more on this in the conclusion...)
Implementation
Overview
Role Playing
Variants
Mutators
Implementation
Getting Started
appendcount
contains
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 26 of 40
Getting Started
Overview
Role Playing
Variants
Mutators
Implementation
Getting Started
appendcount
contains
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 27 of 40
class OurList:def init (self):
self. head = Noneself. rest = None
def isEmpty(self): # a private utilityreturn self. rest is None
The append method
Overview
Role Playing
Variants
Mutators
Implementation
Getting Started
appendcount
contains
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 28 of 40
Has a base case and a simple recursion
def append(self, value):if self. isEmpty( ):
self. head = value # we have one itemself. rest = OurList( ) # followed by empty list
else:self. rest.append(value) # recurse
The count method
Overview
Role Playing
Variants
Mutators
Implementation
Getting Started
appendcount
contains
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 29 of 40
Has a base case and a non-trivial recursion
def count(self, value):if self. isEmpty( ):
return 0else:
answer = self. rest.count(value)if self. head == value: # additional match
answer += 1return answer
The contains method
Overview
Role Playing
Variants
Mutators
Implementation
Getting Started
appendcount
contains
Conclusions
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 30 of 40
Has two distinct base cases
def contains (self, value):if self. isEmpty( ):
return Falseelif self. head == value:
return Trueelse:
return value in self. rest # implicit recursion
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Conclusions
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 31 of 40
■ Lots of fun
■ Strategic challenges
■ Varying recursive patterns
■ Instills a local perspective
■ Coherent transition to source code
■ We have really used functional recursion aswell as structural recursion.
Advanced Lessons
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 32 of 40
■ Ample opportunities for advanced lessons(time permitting)
◆ Error handling
◆ Default parameter values
◆ More complex recursive patterns
Error handling
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 33 of 40
def getitem (self, i):if self. isEmpty( ):
raise IndexError('index out of range')elif i == 0:
return self. headelse:
return self. rest. getitem (i−1)
Error handling
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 34 of 40
def getitem (self, i):if self. isEmpty( ):
raise IndexError('index out of range')elif i == 0:
return self. headelse:
try:return self. rest. getitem (i−1)
except IndexError:raise IndexError('index out of range')
insert and remove
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 35 of 40
def insert(self, index, value):if self. isEmpty( ): # ”append” to end
self. head = valueself. rest = OurList( )
elif index > 0: # insert recursivelyself. rest.insert(index−1, value)
else:# reinsert our head as the front of the restself. rest.insert(0, self. head)# and then store the new value hereself. head = value
The insert method (alternative)
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 36 of 40
def insert(self, index, value):if self. isEmpty( ): # ”append” to end
self. head = valueself. rest = OurList( )
elif index > 0: # insert recursivelyself. rest.insert(index−1, value)
else: # new item goes here!shift = OurList( )shift. head = self. headshift. rest = self. restself. head = valueself. rest = shift
The remove method
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 37 of 40
def remove(self, value):if self. isEmpty( ):
raise ValueError('value not in list')elif self. head == value:
self. head = self. rest. head # privateself. rest = self. rest. rest # private
else:self. rest.remove(value)
Default parameters
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 38 of 40
def pop(self, index=None):if self. isEmpty( ):
raise IndexError('pop from empty list')else:
if index is None:index = len(self) − 1
if index == 0:answer = self. headself. head = self. rest. headself. rest = self. rest. restreturn answer
else:return self. rest.pop(index−1)
reverse
Overview
Role Playing
Variants
Mutators
Implementation
Conclusions
Conclusions
Advanced Lessons
Error handling
insert andremove
Default parameters
reversesort
2007 OOPSLA Educators’ Symposium Reinforcing Structural Recursion with Lists – 39 of 40
Make use of other existing methods together withone recursive call.
def reverse(self):if not self. isEmpty( ):
self. rest.reverse( )self. rest.append(self. head)self.remove(self. head)