arbitrarily long data structures: lists and recursion cmsc 11500 introduction to computer...

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

Upload: brett-long

Post on 28-Dec-2015

216 views

Category:

Documents


4 download

TRANSCRIPT

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

Arbitrarily Long Data Structures:

Lists and RecursionCMSC 11500

Introduction to Computer Programming

October 4, 2002

Page 2: 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

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

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

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

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

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

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

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

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)

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

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

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

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

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

Selector Examples

• (define list1 (cons 1 (cons 2 (cons 3 nil))))

• (car list1)

• (car (cdr list1))

• (cdr (cdr (cdr list1)))

• (car (cdr (cdr list1)))

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

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)

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

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

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

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

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

Stepping through lists: Length

(define (length alist)

(if (null? alist)

0

(+ 1 (length (cdr alist)))))