Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \ 7 9 2 4 7 9 2 4.

Slides:



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

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CMSC 202 Fall 2010, Advanced Section Designing with Objects.
Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section &
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
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.
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
CS 201 Functions Debzani Deb.
מבוא מורחב - שיעור 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.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Fall 2008Programming Development Techniques 1 Topic 13 Multiple Representations of Abstract Data – Tagged Data Section
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 CS161 Introduction to Computer Science Topic #9.
LET’S EDIT! MONDAY, NOVEMBER 5 TH, SPONGE Complete Grammar for Writing Workbook page 7, #1-10. You will need your Source Books today!!!
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
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.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
Controlling Program Flow with Decision Structures.
OOP Basics Classes & Methods (c) IDMS/SQL News
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
Object Oriented Programming Systems
Tagged Data Tag: a symbol in a data structure that identifies its type
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lesson 2: Building Blocks of Programming
Selection CIS 40 – Introduction to Programming in Python
Functions BIS1523 – Lecture 17.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
Coding Concepts (Basics)
Lecture 16: Tables and OOP
6.001 SICP Further Variations on a Scheme
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
6.001 SICP Variations on a Scheme
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 13: Assignment and the Environment Model (EM)
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \

Administrivia Midterm is in < week.  Wednesday March 10, 7pm 1 Pimentel  Open Book Open Notes  No Group Portion Midterm Review (possibility on Monday) held by Tyler and Alex.

Data Directed Programming …let that data flow…

Intro to DDP Computers are not intelligent. By using different levels of Data Abstraction, we can make different levels of the computer hierarchy act intelligently. With Data Directed Programming (DDP) we can make our data "informative" to our "smart" generic procedures, and have it dictate the flow of our program. Instead of having generic data where we apply a generic function, we can have specific data that forces a generic program to act like a type specific function.

Example… Lets look at an example of our boring generic function perimeter, that cannot be expanded to a generic function. (define (sq-perimeter square) (* 4 (get-side square))) (define (ci-perimeter circle) (* pi (get-diameter cirle))) What’s wrong with it?

Example… VERY INEFFICIENT! Need to know the type of data being passed to each procedure Let’s create a general function that knows what kind of procedure to apply on a specific shape

Idea of DDP… The idea of DDP is that the data is still kind of dumb, but it knows what type it is. (define (tag type data) (cons type data)) (define (type-tag data) (car data)) (define (contents data) (cdr data)) (define (make-square side) (tag 'square side)) (define (make-circle diameter) (tag 'circle side)) (define (generic-perimeter obj) (let ((type (type-tag data)) (data (contents obj))) (cond ((equal? type 'square) (* 4 data)) ((equal? type 'circle) (* pi data)) (else (error "Bad type of object")))))

Idea of DDP… Now when we create squares and circles using tag on our data, generic-perimeter will apply the correct perimeter function to the data. In other words, our data is directing the flow of the function!

More shapes??? What happens if we added more shapes? Well then inside the cond statement we would have to add hundreds of more conditionals and we might mess-up our earlier version of the code. Lets make our procedure smarter! We can store functions (or values) in a table and then simply retrieve the appropriate function depending on the type of the object. Lets look at our table commands:

The Global Table… Table A 2-dimensional table that stores your information. Put Places a value (or function) in a specific slot in your table. It's syntax is (put x-val y-val value). Where value is stored in the "box" (x-val, y-val) Get Gets your entry out of the table and returns the value you stored there (or #f if there is nothing at that point in the table). The syntax is (get x-val y-val). Where you return whatever is in the box at (x-val, y-val) or #f if nothing was stored there

Using the table… Now we can just store all the perimeter functions in a table and apply them to the correct arguments. Also we can write area functions that apply the correct area function for its shape! Remember, when we return a function we should use lambda! (put 'square 'perimeter (lambda (side) (* side 4))) (put 'square 'area (lambda (side) (* side side))) (put 'circle 'perimeter (lambda (diameter) (* pi diameter))) (put 'circle 'area (lambda (diameter) (* (* (/ diameter 2) (/ diameter 2)) pi))) (define (perimeter shape) (really-generic shape 'perimeter)) (define (area shape) (really-generic shape 'area)) (define (really-generic shape op-type) (let ((proc (get (type-tag shape) op-type))) (if proc (proc (contents shape)) (error "Bad type"))))

Recap of what we know… We’ve seen functional programming.  Good for small programs that are easy to follow Now we have Tagged Data & DDP  Data can now dictate the flow of the program  Data is ‘informative’ & procedures are ‘smart’ So…what’s message passing?

Brief Intro to Message Passing Similar idea to data directed programming, but this time data isn’t just informative. It’s SMART! More so a smart procedure! So to do something, all we need to do is ask the data to do it. So we pass the data a message.

Brief Intro to Message Passing What’s a message?  Also known as a dispatch  An interface to the to the program, or data So you don’t need to know how it’s implemented, just how it works. Aha! Abstraction!

Brief Intro to Message Passing Let’s take a look at MP in action! (define (number n) (lambda (message) (cond ((equal? message 'add) (lambda (x) (+ x n))) ((equal? message 'sub) (lambda (x) (- n x))) ((equal? message 'scale) (lambda (x) (* n x))) (else (se '(sorry, I don't know how to ) message))))) (define (make-imaginary x y) (lambda (message) (cond ((equal? message 'real) x) ((equal? message 'imag) y) ((equal? message 'add) (lambda (z) (make-imaginary (+ (z 'real) x) (+ (z 'imag) y)))) ((equal? message 'sub) (lambda (z) (make-imaginary (- (z 'real) x) (- (z 'imag) y)))) ((equal? message 'scale) (lambda (n) (make-imaginary (* n x) (* n y)))))))

Brief Intro to Message Passing So numbers and imaginary numbers know how to add, subtract, and scale instances of themselves from/to other numbers. Try it out!  (define two (number 2))  (define 3+4i (make-imaginary 3 4))  two  (two 'add)  ((two 'add) 4)  ((two 'scale) 8)  ((3+4i 'add) (make-imaginary 4 5))  ((3+4i 'sub) (make-imaginary 4 5))

Brief Intro to Message Passing Even though there are completely different ways to add, subtract, etc. different numbers, we can just ask the ‘instance’ of the type of data to perform the task without knowing how to operate imaginary numbers We don’t need to know the implementation, but just know what actions the data can perform (interface) and ask the data to perform the task for us.

DDP vs. MP What’s a dispatch???? With DDP, a dispatch call comes from the call to put, and get. The dispatch returns a procedure to the generic program, which uses the procedure to specifically implement a function. i.e. in DDP a dispatch is the way in which the data tells the generic op what to do. If we want to add another procedure when using DDP, we do not have to change our generic op. All we have to do is give our dispatch another procedure from which to choose. Remember DDP means generic op's, and smart data which pick specific procedures using an explicit dispatch. With Message Passing, the book often uses the convention that a "dispatch" is the message we pass to our argument. This dispatch is similar to the "dispatch" we use with DDP. However, it is an explicit dispatch. This dispatch (or message) does not depend on the type of data, it depends on the availability of the interface. With MP, you must explicitly call the dispatch procedure to work with data. It is difficult to add procedures for which the dispatch can access, when using MP. We actually have to go into the cond clause and add a new phrase to add functionality to our data.

DDP vs. MP Where are the functions stored for each data type?  With DDP you store the procedures in a table (using put/get).  With Message Passing you store the procedures in the data itself (in the lambda expression).

DDP vs. MP Which one’s better? Well that question is quite type specific. Mostly, we want to know which style will provide us easier access to creating and mutating data/procedures, and which provides us the most encapsulation (well-defined abstraction barrier).  With DDP, it is much easier to add procedures. All we use is an explicit call to put, and Bam! our procedure is ready to be called via table lookup (get).  Message Passing is easier to use for encapsulation purposes. If someone implements an object (or class of objects) for us, we do not have to know how to call the procedure. All we have to know is the correct usage of such defined procedures.

Next Time…MP  OOP …don’t you love acronyms