1 / 13

Arbitrarily Long Data Structures: Lists and Recursion

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

Download Presentation

Arbitrarily Long Data Structures: Lists and Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arbitrarily Long Data Structures: Lists and Recursion CMSC 11500 Introduction to Computer Programming October 4, 2002

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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)

  7. 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

  8. 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

  9. Selector Examples • (define list1 (cons 1 (cons 2 (cons 3 nil)))) • (car list1) • (car (cdr list1)) • (cdr (cdr (cdr list1))) • (car (cdr (cdr list1)))

  10. 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)

  11. 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)))))

  12. 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

  13. Stepping through lists: Length (define (length alist) (if (null? alist) 0 (+ 1 (length (cdr alist)))))

More Related