The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There isn't just one sequence type (in Python or in general) CS 61A Lecture 9 This abstraction is a collection of behaviors: Length. A sequence has a finite length. Friday, September 16 Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element. The sequence abstraction is shared among several types. 2 Tuples are Sequences Box-and-Pointer Notation for Nested Pairs (1, 2) 2 Every object is an arrow pointing to a box 1 The number 2 Boxes for tuples have multiple parts ((1, 2), (3, 4)) (Demo) 4 (1, 2) tuple 2 3 1 3 Alternative Nested Structures The Closure Property of Data Types (1, (2, (3, 4))) 4 1 (1, 2, 3, 4) 2 3 ((1, 2), (3, 4)) 4 1 2 3 4 4 2 • A method for combining data values satisfies the closure property if: • The result of combination can itself be combined using the same method. • Closure is the key to power in any means of combination because it permits us to create hierarchical structures. • Hierarchical structures are made up of parts, which themselves are made up of parts, and so on. Tuples can contain tuples as elements 3 1 5 6 Recursive Lists Implementing Recursive Lists with Pairs Constructor: A recursive list is a pair def make_rlist(first, rest): """Make a recursive list from its first element and the rest.""" None represents the empty list Selectors: None def first(s): """Return the first element of a recursive list s.""" def rest(s): 1 2 """Return the rest of the elements of a recursive list s.""" 9_rlist.py Page 1 9_rlist.py Page 1 Behavior condition(s): # Recursive list abstract data type The first element of the pair is the first If a recursive s wasdata constructed from first element f # Recursive list list abstract type empty_rlist = None element of the list and recursive list r, then empty_rlist = None def make_rlist(first, rest): f, and • first(s) areturns recursive list from its first element and the rest.""" def """Make make_rlist(first, rest): return rest)list from its first element and the rest.""" """Make(first, a recursive rest(s) (first, returns rest) r, which is a recursive list. • return def first(s): def """Return first(s): the first element of a recursive list s.""" 7 return s[0] """Return the first element of a recursive list s.""" return s[0] def rest(s): def """Return rest(s): the rest of the elements of a recursive list s.""" return s[1] """Return the rest of the elements of a recursive list s.""" return s[1] # Manipulating recursive lists # Manipulating recursive lists counts = make_rlist(1, make_rlist(2, make_rlist(3, make_rlist(4, empty_rlist)))) first(counts) counts = make_rlist(1, make_rlist(2, make_rlist(3, make_rlist(4, empty_rlist)))) rest(counts) first(counts) Implementing Environment Diagram for rest(counts) the Sequence Abstraction # Implementing the sequence abstraction # Implementing the sequence abstraction def len_rlist(s): first: """Return the length of recursive list s.""" def length len_rlist(s): first = 0 rest: """Return the length of recursive list s.""" while s != empty_rlist: rest length = 0 ... s, length = rest(s), length + 1 (Demo) while slength != empty_rlist: counts: return s, length = rest(s), length + 1 return length def getitem_rlist(s, i): 1 2 """Return the element at index i of recursive list s.""" def while getitem_rlist(s, i): i > 0: s: """Return the element at index i of recursive list s.""" s, i = rest(s), i é 1 i: 1 while ifirst(s) > 0: return s, i = rest(s), i é 1 getitem_rlist return first(s) 3 4 The second element of the pair is the rest of the list (Demo) 8 getitem_rlist None 3 4 getitem_rlist(counts, 1) while i > 0: s, i = rest(s), i - 1 return first(i) Length. A sequence has a finite length. Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element. 9 Environment Diagram for getitem_rlist Environment Diagram for getitem_rlist first: first: first rest: ... counts: None 1 2 3 first rest: ... counts: rest 4 rest None 1 s: i: 1 0 getitem_rlist 10 2 s: i: 1 0 while i > 0: s, i = rest(s), i - 1 return first(i) 4 2 getitem_rlist(counts, 1) getitem_rlist getitem_rlist(counts, 1) 3 while i > 0: s, i = rest(s), i - 1 return first(s) s: first 2 first(s) return s[0] 11 12 Sequence Iteration For Statement Execution Procedure (Demo) for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must yield an iterable value. def count(s, value): total = 0 for elem in s: 2. For each element value in that sequence, in order: Name bound in the first frame of the current environment A. Bind <name> to that value in the local environment. if elem == value: total = total + 1 return total B. Execute the <suite>. 13 14
© Copyright 2026 Paperzz