Lecture 16: Tables and OOP - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 16: Tables and OOP

Description:

elections California Gore table) == okbb. 11. Two dimentional tables. presidents. Clinton ... elections. NY. Florida. Gore. Bush. Bush. California. Gore ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 35
Provided by: duaneb3
Category:
Tags: oop | lecture | tables

less

Transcript and Presenter's Notes

Title: Lecture 16: Tables and OOP


1
Lecture 16 Tables and OOP
2
Tables -- get and put
3
One dimentional tables
(define (lookup key table) (let ((record (assoc
key (cdr table)))) (if record (cdr
record) false)))
4
One dimentional tables
(define (lookup key table) (let ((record (assoc
key (cdr table)))) (if record (cdr
record) false)))
(define (assoc key records) (cond ((null?
records) false) ((equal? key (caar
records)) (car records)) (else (assoc key
(cdr records)))))
5
One dimentional tables
(define (insert! key value table) (let ((record
(assoc key (cdr table)))) (if record
(set-cdr! record value) (set-cdr! table
(cons (cons key value) (cdr
table))))) 'ok)
Example (insert! e 5 table)
6
One dimentional tables
(define (insert! key value table) (let ((record
(assoc key (cdr table)))) (if record
(set-cdr! record value) (set-cdr! table
(cons (cons key value) (cdr
table))))) 'ok)
7
One dimentional tables
(define (make-table)(list 'table))
8
Two dimentional tables
table
presidents
Clinton
Bush
88
92
elections
NY
Gore
Bush
Bush
California
9
Two dimentional tables
(define (lookup key-1 key-2 table) (let
((subtable (assoc key-1 (cdr table)))) (if
subtable (let ((record (assoc key-2 (cdr
subtable)))) (if record
(cdr record) false))
false)))
Example (lookup elections Florida table) gt
Bush
10
Two dimentional tables
(define (insert! key-1 key-2 value table) (let
((subtable (assoc key-1 (cdr table)))) (if
subtable (let ((record (assoc key-2 (cdr
subtable)))) (if record
(set-cdr! record value) (set-cdr!
subtable (cons (cons
key-2 value) (cdr
subtable))))) (set-cdr! table
(cons (list key-1
(cons key-2 value))
(cdr table))))) 'ok)
Example (insert! elections California Gore
table) gt okbb
11
Two dimentional tables
table
presidents
Clinton
Bush
88
92
elections
NY
Gore
Bush
Bush
California
12
Two dimentional tables
Example (insert! singers Madona M table) gt
ok
13
Two dimentional tables
table
Clinton
Bush
88
92
14
Implement get and put
(define oper-table (make-table)) (define (put x y
v) (insert! x y v oper-table)) (define (get x y)
(lookup x y oper-table))
15
Introduction to Object Oriented Programming
16
One View of Data
  • Tagged data
  • Some complex structure constructed from cons
    cells
  • Explicit tags to keep track of data types
  • Implement a data abstraction as set of procedures
    that operate on the data
  • "Generic" operations by looking at types
  • (define (real-part z)
  • (cond ((rectangular? z)
  • (real-part-rectangular (contents z)))
  • ((polar? z)
  • (real-part-polar (contents z)))
  • (else (error "Unknown type -- REAL-PART"
    z))))

17
An Alternative View of Data Procedures with
State
  • A procedure has
  • parameters and body as specified by l expression
  • environment (which can hold name-value bindings!)
  • Can use procedure to encapsulate (and hide) data,
    and provide controlled access to that data
  • constructor, accessors, mutators, predicates,
    operations
  • mutation changes in the private state of the
    procedure

18
Example Pair as a Procedure with State
  • (define (cons x y)
  • (lambda (msg)
  • (cond ((eq? msg CAR) x)
  • ((eq? msg CDR) y)
  • ((eq? msg PAIR?) t)
  • (else (error "pair cannot" msg)))))
  • (define (car p) (p CAR))
  • (define (cdr p) (p CDR))
  • (define (pair? p) (and (procedure? p) (p
    PAIR?)))

19
Example What is our "pair" object?
(define foo (cons 1 2))
20
Pair Mutation as Change in State
  • (define (cons x y)
  • (lambda (msg)
  • (cond ((eq? msg CAR) x)
  • ((eq? msg CDR) y)
  • ((eq? msg PAIR?) t)
  • ((eq? msg SET-CAR!)
  • (lambda (new-car) (set! x new-car)))
  • ((eq? msg SET-CDR!)
  • (lambda (new-cdr) (set! y new-cdr)))
  • (else (error "pair cannot" msg)))))
  • (define (set-car! p new-car)
  • ((p SET-CAR!) new-car))
  • (define (set-cdr! p new-cdr)
  • ((p SET-CDR!) new-cdr))

21
Example Mutating a pair object
  • (define bar (cons 3 4))

22
Message Passing Style - Refinements
  • lexical scoping for private state and private
    procedures
  • (define (cons x y)
  • (define (change-car new-car) (set! x new-car))
  • (define (change-cdr new-cdr) (set! y new-cdr))
  • (lambda (msg . args)
  • (cond ((eq? msg CAR) x)
  • ((eq? msg CDR) y)
  • ((eq? msg PAIR?) t)
  • ((eq? msg SET-CAR!)
  • (change-car (car args)))
  • ((eq? msg SET-CDR!)
  • (change-cdr (car args)))
  • (else (error "pair cannot" msg)))))
  • (define (car p) (p 'CAR))
  • (define (set-car! p val) (p 'SET-CAR! val))

23
Variable number of arguments
  • A scheme mechanism to be aware of
  • Desire
  • (add 1 2)
  • (add 1 2 3 4)
  • How do this?
  • (define (add x y . rest) ...)
  • (add 1 2) gt x bound to 1
  • y bound to 2
  • rest bound to '()
  • (add 1) gt error requires 2 or more
    args
  • (add 1 2 3) gt rest bound to (3)
  • (add 1 2 3 4 5) gt rest bound to (3 4 5)

24
Message Passing Style - Refinements
  • lexical scoping for private state and private
    procedures
  • (define (cons x y)
  • (define (change-car new-car) (set! x new-car))
  • (define (change-cdr new-cdr) (set! y new-cdr))
  • (lambda (msg . args)
  • (cond ((eq? msg CAR) x)
  • ((eq? msg CDR) y)
  • ((eq? msg PAIR?) t)
  • ((eq? msg SET-CAR!)
  • (change-car (car args)))
  • ((eq? msg SET-CDR!)
  • (change-cdr (car args)))
  • (else (error "pair cannot" msg)))))
  • (define (car p) (p 'CAR))
  • (define (set-car! p val) (p 'SET-CAR! val))

25
Programming Styles Procedural vs.
Object-Oriented
  • Procedural programming
  • Organize system around procedures that operate on
    data
  • (do-something ltdatagt ltarggt ...)
  • (do-another-thing ltdatagt)
  • Object-based programming
  • Organize system around objects that receive
    messages
  • (ltobjectgt 'do-something ltarggt)
  • (ltobjectgt 'do-another-thing)
  • An object encapsulates data and operations
  • Message passing and procedure are the means to
    write Object
  • Oriented code in scheme

26
Tables in OO style
(define (make-table) (let ((local-table (list
'table))) (define (lookup key-1 key-2)
. . . ) (define (insert! key-1 key-2
value) . . . 'ok) (define
(dispatch m) (cond ((eq? m 'lookup-proc)
lookup) ((eq? m 'insert-proc!)
insert!) (else (error "Unknown
operation -- TABLE" m)))) dispatch))
27
Table in OO style
(define operation-table (make-table)) (define get
(operation-table 'lookup-proc)) (define put
(operation-table 'insert-proc!))
28
(define oper-table (make-table)) GE

make-table
GE
p b (let ((local-table (list 'table))) . .
. )
29
Object-Oriented Programming Terminology
  • Class
  • specifies the common behavior of entities
  • in scheme, a "maker" procedure
  • E.g. cons or make-table in our previous examples
  • Instance
  • A particular object or entity of a given class
  • in scheme, an instance is a message-handling
    procedure made by the maker procedure
  • E.g. foo or bar or oper-table in our previous
    examples

30
Stacks in OO style
(define (make-stack) (let ((top-ptr '()))
(define (empty?) (null? top-ptr)) (define
(delete!) (if (null? top-ptr) (error . .
.) (set! top-ptr (cdr top-ptr)))
top-ptr ) (define (insert! elmt) (set!
top-ptr (cons elmt top-ptr)) top-ptr)
(define (top) (if (null? top-ptr) (error . . .)
(car top-ptr))) (define
(dispatch op) (cond ((eq? op 'empty?)
empty?) ((eq? op 'top) top) ((eq?
op 'insert!) insert!) ((eq? op 'delete!)
delete!))) dispatch))
31
Stacks in OO style
undef
(define s (make-stack)) gt ((s 'insert!) 'a)
gt ((s 'insert!) 'b) gt ((s 'top)) gt ((s
'delete!)) gt ((s 'top)) gt ((s 'delete!)) gt
(a)
(b a)
b
(a)
a
()
32
Queues in OO style
A lazy approach We know how to do stacks so lets
do queues with stacks ) We need two stacks
stack1
stack2
insert
delete
33
Queues in OO style
((q insert) a)
((q insert) b)
((q delete))
((q insert) c)
((q delete))
34
Queues in OO style
(define (make-queue) (let ((stack1
(make-stack)) (stack2 (make-stack))) (define
(reverse-stack s1 s2) _______________)
(define (empty?) (and ((stack1
'empty?)) ((stack2 'empty?)))) (define
(delete!) (if ((stack2 'empty?))
(reverse-stack stack1 stack2)) (if ((stack2
'empty?)) (error . . .) ((stack2
'delete!)))) (define (first) (if
((stack2 'empty?)) (reverse-stack stack1
stack2)) (if ((stack2 'empty?)) (error . .
.) ((stack2 'top)))) (define (dispatch
op) (cond ((eq? op 'empty?) empty?)
((eq? op 'first) first) ((eq? op 'delete!)
delete!) (else (stack1 op))))
dispatch))
35
Queues in OO style
Inheritance One class is a refinement of
another The queue class is a subclass of the
stack class
Write a Comment
User Comments (0)
About PowerShow.com