+ All Categories
Home > Documents > Lecture 18: Tables and OOP

Lecture 18: Tables and OOP

Date post: 12-Jan-2016
Category:
Upload: helia
View: 45 times
Download: 0 times
Share this document with a friend
Description:
Lecture 18: Tables and OOP. Pair as OOP Delay and force. *table*. a. 1. 2. 3. 4. b. c. d. One dimentional tables. One dimentional tables. ( define (assoc key records) (cond ((null? records) false) ((equal? key (caar records)) (car records)) - PowerPoint PPT Presentation
22
1 Lecture 18: Tables and OOP Pair as OOP Delay and force
Transcript
Page 1: Lecture 18: Tables and OOP

1

Lecture 18: Tables and OOP

Pair as OOP

Delay and force

Page 2: Lecture 18: Tables and OOP

2

One dimentional tables

*table*

c dba 1 2 3 4

Page 3: Lecture 18: Tables and OOP

3

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

Page 4: Lecture 18: Tables and OOP

4

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)

Page 5: Lecture 18: Tables and OOP

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)

*table*

c dba 1 2 3 4e 5

Page 6: Lecture 18: Tables and OOP

6

One dimentional tables

(define (make-table)(list '*table*))

*table*

Page 7: Lecture 18: Tables and OOP

13

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

Page 8: Lecture 18: Tables and OOP

14

Table in OO style

(define operation-table (make-table))(define get (operation-table 'lookup-proc))(define put (operation-table 'insert-proc!))

Page 9: Lecture 18: Tables and OOP

15

(define oper-table (make-table)) | GE

GE

p: b:(let ((local-table (list '*table*))) . . . )

make-table:

lookup:p: key-1 key-2b: . . .

insert!:

oper-table:

dispatch:

E1 local-table

*table*

Page 10: Lecture 18: Tables and OOP

16

Programming Styles – Procedural vs. Object-Oriented

• Procedural programming:• Organize system around procedures that operate on data

(do-something <data> <arg> ...)

(do-another-thing <data>)

•Object-based programming:•Organize system around objects that receive messages (<object> 'do-something <arg>) (<object> 'do-another-thing)•An object encapsulates data and operations•Message passing and procedure are the means to write Object•Oriented code in scheme

Page 11: Lecture 18: Tables and OOP

17

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. oper-table in our previous examples

Page 12: Lecture 18: Tables and OOP

26

Delay and force

Page 13: Lecture 18: Tables and OOP

27

delay and force

(delay <exp>) ==> a promise to evaluate exp

(force <delayed object>) ==> evaluate the delayed object and return the result

(define x (delay (+ 1 1)))x #<promise>(force x) 2

(delay <exp>) is a special form.

force is not a special form.

Page 14: Lecture 18: Tables and OOP

28

What are these mysterious delay and force ?

delay is a special form such that

(delay <exp>) is equivalent to (lambda () <exp>)

force is a procedure that calls a procedure produced by delay:

(define (force delayed-object)

(delayed-object))

Page 15: Lecture 18: Tables and OOP

29

(define x (delay (+ 1 1))) | GE

GE

p: b: (+ 1 1)

x:

(force x) | GE

(x) | GE

2

Page 16: Lecture 18: Tables and OOP

30

Normal (Lazy) order evaluation?

Apply operator with unevaluated argument sub-expressions.

Evaluate a sub-expression only when value is needed – to print– by primitive procedure (that is, primitive procedures

are "strict" in their arguments)

Page 17: Lecture 18: Tables and OOP

31

Normal order evaluation does not go well with mutators.

Consider:

(set! a 4) (set! b (delay (+ 1 a))) (set! a 0) (force b)

The value of a delayed operation depends on the actual time it is called,And this can be very confusing.

Scheme does not use Normal order evaluation, except forstreams, delay and force.

Page 18: Lecture 18: Tables and OOP

32

Forcing a delayed object many times

Suppose we have the following scenario:

(define x (delay (very-hard-function a)))(force x)(force x)

We need to call the hard function twice.

Scheme will automatically detect that this is the second time we try to evaluate the funtion and use the value we have evaluated before.

Scheme makes it look like:(define x (delay (very-hard-function a)))(define b (force x))bb

Page 19: Lecture 18: Tables and OOP

33

Beware: counter intuitive feature

(define a 1)

(define x (delay a))

(set! a 2)

(force x)

(set! a 3)

(force x)

The result is 2 (rather than 3)

Page 20: Lecture 18: Tables and OOP

34

How is it done?

We redefine delay as follows

(delay <exp>) translates to (memo-proc (lambda () <exp>))

(define (memo-proc proc) (let ((already-run? false) (result false)) (lambda () (if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result))))

Page 21: Lecture 18: Tables and OOP

35

(define a 1) | GE

a:1

(define x (delay a)) | GE

GE

p:proc

b:(let .. (lambda () (if (not already-run?)…

Memo_proc:

(define x (memo-proc (lambda () a))) | GE

p:

b:a

already-run: #f result: #f

p:

b:(if (not already-run …

x:

proc:

Page 22: Lecture 18: Tables and OOP

36

proc:

already-run: #f result: #f

a:1GE

p:proc

b:(lambda () (if (not already-run?)…

Memo_proc:

p:

b:a

p:b:(if (not already-run …

x:

(set! a 2) | GE

2

(force x) | GE

#t 2

(set! a 3) | GE

3

(force x) | GE


Recommended