Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
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).
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
1 Lecture 15: More about assignment and the Environment Model (EM)
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Functional Programming: Lisp MacLennan Chapter 10.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1/32 Data Mutation Primitive and compound data mutators set! for names set-car!, set-cdr! for pairs Stack example non-mutating mutating Queue example non-mutating.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Additional Scheme examples
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
CS 550 Programming Languages Jeremy Johnson
6.001 Jeopardy.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
6.001 SICP Data abstractions
Lecture #8 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Data Mutation Primitive and compound data mutators set! for names
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).
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Lisp.
More Scheme CS 331.
Lists in Lisp and Scheme
Presentation transcript:

Data Abstraction… The truth comes out…

What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion

Administrivia! Midterm 1 will be graded by Saturday. Expect seeing a grade some time this weekend. Project 1 grades should be coming in soon. Just hold onto your horses =)

Abstraction The BIGGEST idea of this course Ability to hide the lower levels of detail Example:  Driving a car but not knowing how it really runs  Using sentences and words, but not knowing exactly how it’s implemented….until now.

Abstract Data Type (ADT) is… the logical data structure itself (an abstraction, not the detailed implementation), combined with… a set of operations which work on the data structure. When we use ADTs, we don’t care how they’re implemented, just how to use them.

ADT: The Dotted Pair What is a Pair?  Most basic data structure  Puts two things together

ADT: The Dotted Pair Constructor:  Cons: (cons ) Example:  (cons 1 2) => (1. 2)

ADT: The Dotted Pair Selectors:  Car: (car ) Examples:  (car (cons 1 2)) => 1  Cdr: (cdr ) Examples:  (cdr (cons 1 2)) => 2

ADT: Lists 1/7 What are Lists?  An ordered set of elements enclosed in ‘( )’  Built on cons cells, so it’s a pair whose ‘cdr’ is the empty list (list... ) => (cons … (cons nil))

ADT: List 2/7 Difference between lists and sentences?  A sentence can contain only words and sentences  A list can contain anything: Booleans Procedures Other lists Sentences can be thought of as a “flat” list. They both have their own set of constructors and selectors.

ADT: Lists 3/7 More Constructors & Examples:  Cons Examples:  (cons ‘a ‘b) => (a. b)  (cons (cons a ‘()) (cons b (cons c ‘()))) => ((a) b c)  List Examples:  (list ‘a ‘b) => (a b)  (list (cons a (list b) (list c)) => ((a b) (c))  Append: *always* takes in lists. Examples:  (append (list a b) (list c d)) => (a b c d)  (append (list (list a b)) (list c d)) => ((a b) c d)

ADT: Lists 4/7 Higher Order Functions  Map (like every) Usage: (map ) Example:  (map (lambda (x) (list x)) ‘( )) => ((1) (2) (3) (4))  Filter (like keep) Usage (filter )  (filter list? ‘(a (b c) () ((d))) => ((b c) () ((d)))  Reduce (like accumulate) Usage (reduce )  (reduce (lambda (x y) (if (> (length x) (length y)) x y)) ‘(a (b c d) () ((e))) => (b c d)

ADT: Lists 5/7 More Primitives for Lists!  length: returns the number of elements in a list (like count) Usage: (length )  null?: returns #t if it’s an empty list otherwise #f (like empty?) Usage: (null? )  list?: returns #t if argument is a list, #f otherwise Usage: (list? )

ADT: Lists 6/7  list-ref: returns the element at that position where the first element is the 0 th position. (like item) Usage: (list-ref )  equal?: works the same way as with sentences. Usage: (equal? )  member: returns the part of the list starting with the element, otherwise #f Usage: (member )

ADT: Lists 7/7 Use them correctly!  Examples: (se (list ‘this ‘is ‘bad) (list 1 2 3)) (first (list ‘this ‘is ‘a ‘list)) (car (se ‘this ‘is ‘(a sentence))) Yes they may produce the correct results but it’s a…

DAV! Data Abstraction Violation You’ll probably only hear this in 61a, but… We will DING you guys on this on…  Homework  Projects, and especially  Exams!

ADTs Whenever creating a new ADT make sure:  You have constructors to create that data type  You have selectors for that data type Example:  Create a new data type car which takes a driver, navigator, and a passenger  Create appropriate constructors and selectors for this data type.  Create a procedure run-around which returns a new car with the driver as the old car’s navigator, the navigator as the passenger, and the passenger as the driver.

Box and Pointers 1/5 Back to Pairs  (cons ‘man ‘woman) ;; a pair cons simply makes pairs One Element List  (cons ‘alone ‘()) ;; a list cons to null at the end makes list ‘man ‘woman ‘alone

Box and Pointers 2/5 Tips on how to do box and pointer diagrams.  See how many elements are in the list. This is the “backbone” of the box and pointer diagram.  Whenever you see cons…draw a box.  If you can write out the whole ‘cons’ representation of the list. This will show you how many boxes you’ll be drawing.  Draw a starting arrow to the first box.  Just take it by ‘car’ and ‘cdr’ and you’ll be fine!  Whenever you see a dotted pair, it means that it’s not a list, so it shouldn’t end with a null box.

Box and Pointers 3/5 A Longer List  (cons ‘neo (cons ‘trinity (cons ‘morpheus ‘()))) Equivalent to (list ‘neo ‘trinity ‘morpheus) Whenever you see the list procedure, go ahead and build a ‘backbone’ ‘neo ‘trinity ‘morpheus

Box and Pointers 4/5 A Nested List  (cons (list ‘greg ‘carolen ‘alex) (list ‘kurt ‘brian)) ‘((greg carolen alex) kurt brian) ‘kurt ‘brian ‘greg‘carolen ‘alex

Box and Pointers 5/5 Just to make sure you didn’t forget about pairs… What scheme expression to construct... ‘architect ‘oracle ‘ghost ‘keymaker

Chasing cars… and cdrs? 1/3 car means follow the first arrow cdr means follow the second arrow ‘man ‘woman

Chasing cars… and cdrs? 2/3 (define x ‘((greg carolen alex) kurt brian)) (car (cdr (car x))) ;; what is this? ‘kurt ‘brian ‘greg‘carolen ‘alex x

Chasing cars… and cdrs? 3/3 (define x ‘((greg carolen alex) kurt brian)) (car (cdr (car x))) ;; what is this? ‘kurt ‘brian ‘greg‘carolen ‘alex x

List, List, List…(List recursion!) List recursion is the same as sentence recursion, but using the list constructors and selectors.  Example: (define (foo lst) (if (null? lst) nil (cons (car lst) (foo (cdr lst))))) (define (foo2 lst) (if (null? lst) nil (append (list (car lst)) (foo (cdr lst))))) Reversing a list is a little bit different than with sentences. This was in lab. Once you get enough practice with list recursion, it’ll become second nature.

Deep List Recursion 1/3 This is really simple. Just do normal recursion, and stick in another base case that asks if the argument is a list. Example:  (define (square-list L) (if (null? L) nil (cons (square (car L)) (square-list (cdr L)))))  (define (square-deep-list L) (cond ((null? L) nil) ((list? (car L)) (cons (square-deep-list (car L)) (square-deep-list (cdr L)))) (else (cons (square (car L)) (square-deep-list (cdr L))))))

Deep Recursion 2/3 Write a function ‘rev’ that reverses a list  (rev ‘(1 2 (3 4))) => ((3 4) 2 1) Now make it so that it does a deep reverse  (deep-rev ‘(1 2 (3 4))) => ((4 3) 2 1)

Deep Recursion 3/3 Answer  (define (rev l) (if (null? l) ‘() (append (rev (cdr l)) (list (car l)))))  (define (deep-rev l) (cond ((null? l) '()) ((list? (car l)) (append (deep-rev (cdr l)) (list (deep-rev (car l))))) (else (append (deep-rev (cdr l)) (list (car l))))))

More Problems… Box and Pointer Practice! (Write what each evaluates to and the box and pointer for each expression)  (cons (list 4 5) 6)  (append (cons 4 ‘( ) ) (list 9))  (list 4 (list 5) (list 6)) Write the cons representation of this list and the box and pointer:  (2 ( (3) 4) ((5. 6)) 7. 8)

More Practice Answers! 1/2 (cons (list 4 5) 6)  ( (4 5). 6) (append (cons 4 ‘( ) ) (list 9))  (4 9)

More Practice Answers 2/2 (list 4 (list 5) (list 6))  (4 (5) (6)) (2 ((3) 4) ((5. 6)) 7. 8)  (cons 2 (cons (cons (cons 3 nil) (cons 4 nil)) (cons (cons (cons 5 6) nil) (cons 7 8))))

Have a Great Weekend! Relax you’ve earned it!...full of scheme fun