lecture 18: tables and oop

Post on 12-Jan-2016

45 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

1

Lecture 18: Tables and OOP

Pair as OOP

Delay and force

2

One dimentional tables

*table*

c dba 1 2 3 4

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

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)

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

6

One dimentional tables

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

*table*

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

14

Table in OO style

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

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*

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

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

26

Delay and force

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.

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

29

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

GE

p: b: (+ 1 1)

x:

(force x) | GE

(x) | GE

2

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)

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.

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

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)

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

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:

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

top related