130 likes | 143 Views
Arbitrarily Long Data Structures: Lists and Recursion. CMSC 11500 Introduction to Computer Programming October 4, 2002. Roadmap. Recap: Compound Data & Abstraction Arbitrary Length Compound Data Lists: Self-referential data structures Definition, Constructors, and selectors
E N D
Arbitrarily Long Data Structures: Lists and Recursion CMSC 11500 Introduction to Computer Programming October 4, 2002
Roadmap • Recap: Compound Data & Abstraction • Arbitrary Length Compound Data • Lists: Self-referential data structures • Definition, Constructors, and selectors • Manipulating arbitrary length data • Recursion: Self-referential procedures • Summary
Recap • Compound Data & Abstraction • Beyond numbers • Structures combine facets of data • Example: (define-struct posn (x y)) • Constructor: (make-posn x y) • Selectors: posn-x, posn-y • Establish abstraction barrier between users of data and implementors of data • Makes code easier to extend and maintain
Limitation of Structures • Fixed length • E.g. posn: x,y; book: ISBN, title, author • What about store inventory? • Arbitrarily large • No predefined size of inventory • Might want to add items over time • Not representable as structure
Solution: Lists • Arbitrary length • List of data objects • e.g. grocery list, inventory list, etc… • Any type (or variety of types) of data • Strings, numbers, booleans • Structures • Even other lists
Solution: Lists • Base list: • nil: the empty list, ‘(), empty • Building a list: Constructor: cons • (cons ‘juice nil) • (cons ‘bread (cons ‘juice nil)) • (cons ‘milk (cons ‘bread (cons ‘juice nil))) • Selecting elements of list: car/first; cdr/rest • (car (cons ‘juice nil)) = > juice • (cdr (cons ‘bread (cons ‘juice nil))) => • (cons ‘juice nil)
Data Definitions for Lists • A list-of-symbols is • 1: the empty list, nil, or • 2: (consslos), where s is a symbol and los is a list of symbols • Note: this definition is self-referential, recursive • Note: In general, lists need not be of only one type - mix types - any type
Examples • Build a list of the planets in the solar system • Make a list of the odd numbers under 10 • Build a list of course books • course, book title, author
Selector Examples • (define list1 (cons 1 (cons 2 (cons 3 nil)))) • (car list1) • (car (cdr list1)) • (cdr (cdr (cdr list1))) • (car (cdr (cdr list1)))
Shorthand for Lists • (cons ‘milk (cons ‘bread (cons ‘juice nil))) • Is equivalent to: • (list ‘milk ‘bread ‘juice) • ‘(milk bread juice) • All evaluate to • (milk bread juice)
Manipulating Lists • Can be input to or output from procedures • Example: (element-of? X alist) • Determine if x is in the list alist • Assume symbol (define (element-of? X alist) (cond ((null? alist) #f) ((equal? X (car alist)) #t) (else (element-of? X (cdr alist)))))
Stepping through lists: Recursion (define (element-of? X alist) (cond ((null? alist) false) ((equal? X (car alist)) true) (else (element-of? X (cdr alist))))) • (null? alist) : true if alist is empty list • Need condition for each case in definition: • Empty list & compound (recursive) list • May need case for each piece of compound • Empty list: “Base” case: stopping condition • O.W. true of 1st? -> true; else check rest • NOTE: Recursive procedure: calls itself
Stepping through lists: Length (define (length alist) (if (null? alist) 0 (+ 1 (length (cdr alist)))))