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

Slides:



Advertisements
Similar presentations
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Advertisements

1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
1 Lecture 15: More about assignment and the Environment Model (EM)
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
Common lisp A functional programming language. Useful URL:
1 Lecture 16: Lists and vectors Binary search, Sorting.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Compound Data. Last Lecture Booleans Only two elements Symbols Not to be confused with variables Strings Notions of equality on values.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Functional Programming Language 1 Scheme Language: part 2.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Functional Programming Language 1 Scheme Language: part 3.
CS314 – Section 5 Recitation 10
Data Abstraction: Sets
CS 550 Programming Languages Jeremy Johnson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Introduction to Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CS 270 Math Foundations of CS Jeremy Johnson
6.001 SICP Data abstractions
The Metacircular Evaluator
The Metacircular Evaluator
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
Rules of evaluation The value of a number is itself.
The Metacircular Evaluator (Continued)
Lecture #9 מבוא מורחב.
Data Mutation Primitive and compound data mutators set! for names
Lecture #6 section pages pages72-77
Let's Play "What's the Question"
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Abstraction and Repetition
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Presentation transcript:

Arbitrarily Long Data Structures: Lists and Recursion CMSC 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: (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

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

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