CS61A Lecture 13 2011-07-12 Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section &
Fractions – Adding Fractions
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
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)))
Copyright © Ed2Net Learning, Inc.1 Compare & Order Rational Numbers / Irrational Numbers #2.
Fractions, Decimals, and Percent By: Jack M 1 _____ %
60 cm : 200 cm can be written as the fraction . 60 cm 200 cm
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
Fall 2008Programming Development Techniques 1 Topic 13 Multiple Representations of Abstract Data – Tagged Data Section
1/38. 2/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
11.6 Adding and Subtracting Rational Expressions
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.
CS61A Lecture Colleen Lewis. Clicker Test When do you think homework should be due? A)10pm B)11pm C)Midnight D)11am the next day.
Reverse Subtraction Objectives:  do a subtract by adding  check your answer by adding.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you allowed to talk about the midterm on piazza or in public yet? A)Yes B)No 2.
CS61A Lecture Colleen Lewis. Clicker poll How do you feel about the “talk to your neighbor” stuff during lecture A)Like it B)It is okay.
CS61A Lecture Colleen Lewis. Clicker poll Do you feel comfortable posting questions to piazza? A)Yes with my classmates and instructors.
1/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.
Perimeter & Area. Today’s Objectives:  Learn what it means to find perimeter and area.  Practice finding or estimating the perimeter and area.
SICP Tagged data Why do we need tags Concept of tags Extended example.
SOLVING ALGEBRAIC EXPRESSIONS
Tagged Data Tag: a symbol in a data structure that identifies its type
6.001 SICP Object Oriented Programming
Programming Languages Dan Grossman 2013
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
You will need some paper!
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
Lecture #8 מבוא מורחב.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Lecture 16: Tables and OOP
6.001 SICP Further Variations on a Scheme
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Mutators for compound data Stack Queue
Lecture 14: The environment model (cont
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
Lecture #7 מבוא מורחב.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Adding fractions with like Denominators
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

CS61A Lecture Colleen Lewis

Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder C)About the same D)A little less hard E)Less hard

get and put Demo

get and put STk> (put 'richmond 'berkeley '$1.75) ok STk> (get 'richmond 'berkeley) $1.75 STk> (get 'berkeley 'richmond) #f The reverse isn’t automatically added. Returns #f if there is no entry

get and put STk> (put 'berkeley 'fremont '$4.30) ok STk> (get 'berkeley 'fremont) $4.30 We can add lots of things with the same first word

get and put (put 'berkeley 'richmond '$1.75) (put 'richmond 'berkeley '$1.75) (put 'berkeley 'fremont '$4.30) (put 'fremont 'berkeley '$4.30) (put 'berkeley 'SFO '$8.65) (put 'SFO 'berkeley '$8.65) (define (bart-cost stop1 stop2) (get stop1 stop2))

get and put STk> (bart-cost 'fremont 'berkeley) $4.30 STk> (bart-cost 'fremont 'SFO) #f

put creates a table BerkeleyFremont$4.30 Richmond$1.75 SFO$8.65 SFOBerkeley$8.65 RichmondBerkeley$1.75 FremontBerkeley$4.30 SFO$10.55 Richmond$4.85

How do I add more Bart Stops? (define (bart-cost stop1 stop2) (get stop1 stop2)) A)Modify the function bart-cost. B)Call put for more stops C)All of the above D)None of the above E)??

Tagged Data

Removing Ambiguity – Tagging data What does this represent? A)¾ B)3+4i C)3x4 rectangle 3 carcdr 4 carcdr 3 carcdr 4 “The next thing is a fraction where the car is the numerator and the cdr is the denominator”

attach-tag (define (attach-tag type contents) (cons type contents)) carcdr Type goes here contents

Selectors (define (type-tag tagged-data) (car tagged-data)) (define (contents tagged-data) (cdr tagged-data))

tagging data (define (make-square side) (attach-tag ‘square side)) (define square3 (make-square 3)) (define square5 (make-square 5)) carcdr square 3 carcdr square 5

tagging data (define (make-circle side) (attach-tag ‘circle side)) (define circle4 (make-circle 3)) (define circle6 (make-circle 5)) carcdr circle 4 carcdr circle 6

Write area to work for circles and squares (define (area shape) How many times did you call type-tag? A)1 B)2 C)3 D)4 E)5 carcdr square 3 carcdr square 5 carcdr circle 6

area solution (define (area shape) (cond ((eq? (type-tag shape) 'square) (* (contents shape) (contents shape))) ((eq? (type-tag shape) 'circle) (* pi (contents shape) (contents shape))) (else (error "Unknown shape --- AREA"))))

How do you call area ? A) STk> ((make-square 4) 'area) 16 B) STk> (area (make-square 4)) 16 C) STk> (area (contents (make-square 4))) 16

What is returned by make-square ? A)A shape B)A pair, where the car is the word square. C)Tagged data D)A function

Could you re-write area ? (put 'square 'area (lambda (s) (* s s))) (put 'circle 'area (lambda (r) (* pi r r))) STk> (area (make-square 3)) 9 Show the answer with: A) Live coding B) PowerPoint C) Chalk

area2 solution (put 'square 'area (lambda (s) (* s s))) (put 'circle 'area (lambda (r) (* r r))) (define (area2 shape) ((get (type-tag shape) 'area) (contents shape)))

How do I add another shape? (define (area2 shape) ((get (type-tag shape) 'area) (contents shape))) A)Modify the function area2. B)Call put for more shapes C)All of the above D)None of the above E)??

perimeter (put 'square 'area (lambda (s) (* s s))) (put 'circle 'area (lambda (r) (* pi r r))) (put 'square 'perimeter (lambda (s) (* 4 s))) (put 'circle 'perimeter (lambda (r) (* 2 pi r))) (define (area2 shape) ((get (type-tag shape) 'area) (contents shape))) (define (perimeter2 shape) ((get (type-tag shape) 'perimeter) (contents shape)))

Error checking: operate (define (operate op obj) (let ((proc (get (type-tag obj) op))) (if proc (proc (contents obj)) (error "Unknown op for type")))) (define (area3 shape) (operate 'area shape))

area data directed solution w/out and w/ put (define (area shape) (cond ((eq? (type-tag shape) 'square) (* (contents shape) (contents shape))) ((eq? (type-tag shape) 'circle) (* pi (contents shape) (contents shape))) (else (error "Unknown shape --- AREA")))) (define (area3 shape) (operate 'area shape)) (put 'square 'area (lambda (s) (* s s))) (put 'circle 'area (lambda (r) (* pi r r)))

Message Passing VERY DIFFERENT

Message passing make-square (define (make-square-mp side) (lambda (message) (cond ((eq? message 'area) (* side side)) ((eq? message 'perimeter) (* 4 side)) (else (error "unknown msg")))))

How do you call area ? A) STk> ((make-square-mp 4) 'area) 16 B) STk> (area (make-square-mp 4)) 16 C) STk> (area (contents (make-square-mp 4))) 16

Easier way to call area (define (make-square-mp side) (lambda (message) (cond ((eq? message 'area) (* side side)) ((eq? message 'perimeter) (* 4 side)) (else (error "unknown msg"))))) (define (area shape) (shape ‘area))

Message passing versus data directed? The constructors for this style return tagged data: A)Data Directed B)Message Passing C)Both D)Neither

Message passing versus data directed? This method CAN use put/get tables A)Data Directed B)Message Passing C)Both D)Neither

Message passing versus data directed? This method used put/get tables in today’s lecture A)Data Directed B)Message Passing C)Both D)Neither

Message passing versus data directed? The constructors for this style return a function: A)Data Directed B)Message Passing C)Both D)Neither

Message passing versus data directed? If you don’t use put, there are a bunch of cond cases to handle each type of data in any “operation” e.g. area : A)Data Directed B)Message Passing C)Both D)Neither

Message passing versus data directed? If you don’t use put, there are a bunch of cond cases to handle each type of “operation” for any data (e.g. circle): A)Data Directed B)Message Passing C)Both D)Neither

Define count-pairs STk>(count-pairs (list )) 4 STk>(count-pairs (list (cons 1 2) 3 4)) 4 (define (count-pairs struct) (if (not (pair? struct)) 0 (+ 1 (count-pairs (car struct)) (count-pairs (cdr struct)))))