Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arbitrarily Long Data Structures: Lists and Recursion CMSC 11500 Introduction to Computer Programming October 4, 2002."— 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: (cons s los), 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 (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 (define (element-of? X alist) (cond ((null? alist) false) ((equal? X (car alist)) true) (else (element-of? X (cdr alist)))))

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


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

Similar presentations


Ads by Google