מבוא מורחב למדעי המחשב בשפת Scheme

35
בבבב בבבבב בבבבב בבבבב בבבבScheme בבבבב8

description

מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 8. Outline. The special form quote Data abstraction: Trie Alternative list: Triplets Accumulate-n. 1. The Special Form quote. quote. Number: does nothing '5=5 Name: creates a symbol 'a = (quote a) => a - PowerPoint PPT Presentation

Transcript of מבוא מורחב למדעי המחשב בשפת Scheme

Page 1: מבוא מורחב למדעי המחשב בשפת  Scheme

מבוא מורחב למדעי המחשבSchemeבשפת

8תרגול

Page 2: מבוא מורחב למדעי המחשב בשפת  Scheme

Outline

1. The special form quote

2. Data abstraction: Trie

3. Alternative list: Triplets

4. Accumulate-n

Page 3: מבוא מורחב למדעי המחשב בשפת  Scheme

1. The Special Formquote

3

Page 4: מבוא מורחב למדעי המחשב בשפת  Scheme

quote

• Number: does nothing

'5=5

• Name: creates a symbol

'a = (quote a) => a

• Parenthesis: creates a list and recursively quotes

'(a b c) = (list 'a 'b 'c) =

= (list (quote a) (quote b) (quote c)) => (a b c)

4

Page 5: מבוא מורחב למדעי המחשב בשפת  Scheme

quote

'a => a (symbol? 'a) => #t (pair? 'a) => #f ''a => 'a (symbol? ''a) => #f (pair? ''a) => #t (car ''a) => quote (cdr ''a) => (a) ''''a => '''a (car ''''a) => quote (cdr ''''a) => (''a)

5

Page 6: מבוא מורחב למדעי המחשב בשפת  Scheme

The predicate eq?

– A primitive procedure that tests if the pointers representing the objects point to the same place.

– Based on two important facts:• A symbol with a given name exists only once. • Each application of cons creates a new pair, different

from any other previously created.

(eq? ‘a ‘a) #t (eq? ‘(a b) ‘(a b)) #f

6

Page 7: מבוא מורחב למדעי המחשב בשפת  Scheme

The predicate equal?

A primitive procedure that tests if the pointers represent identical objects

1. For symbols, eq? and equal? are equivalent

2. If two pointers are eq?, they are surely equal?

3. Two pointers may be equal? but not eq?

(equal? ‘(a b) ‘(a b)) #t

(equal? ‘((a b) c) ‘((a b) c)) #t

(equal? ‘((a d) c) ‘((a b) c)) #f

7

Page 8: מבוא מורחב למדעי המחשב בשפת  Scheme

eq? vs. equal? (symbols)

(eq? ‘a ‘a) #t(equal? ‘a ‘a) #t

(define x ‘a)

(define y ‘a)(eq? x y) #t(equal? x y) #t

8

Page 9: מבוא מורחב למדעי המחשב בשפת  Scheme

(eq? (list 1 2 3) (list 1 2 3)) #f (equal? (list 1 2 3) (list 1 2 3)) #t

(define x (list 1 2 3))(define y (list 1 2 3)) (eq? x y) #f(define z y) (eq? z y) #t (eq? x z) #f

eq? vs. equal? (symbols)

9

Page 10: מבוא מורחב למדעי המחשב בשפת  Scheme

Split

> (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n))

> (split syms ‘-)

((p l a y) (i n) (e u r o p e) (o r) (i n) (s p a i n))

10

Page 11: מבוא מורחב למדעי המחשב בשפת  Scheme

Split

(define (split symbols sep)

(define (update sym word-lists)

(if (eq? sym sep)

(cons ___________________________________

___________________________________ )

(cons ___________________________________

___________________________________)))

(accumulate update (list null) symbols))

null

word-lists

(cons sym (car word-lists))

(cdr word-lists)

11

Page 12: מבוא מורחב למדעי המחשב בשפת  Scheme

Replace

> (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n))

> (replace ‘n ‘m syms)

(p l a y – i m – e u r o p e – o r – i m – s p a i m)

(define (replace from-sym to-sym symbols)

(map

))

(lambda (s) (if (eq? from-sym s) to-sym s))

symbols)

12

Page 13: מבוא מורחב למדעי המחשב בשפת  Scheme

Accum-replace

> (accum-replace ‘((a e) (n m) (p a)) syms)

(p l a y – i n – e u r o p e – o r – i n – s p a i n)

(a l a y – i n – e u r o a e – o r – i n – s a a i n)

(a l a y – i m – e u r o a e – o r – i m – s a a i m)

(e l e y – i m – e u r o e e – o r – i m – s e e i m)

13

Page 14: מבוא מורחב למדעי המחשב בשפת  Scheme

Accum-replace

(define (accum-replace from-to-list symbols)

(accumulate

(lambda(p syms)

( ________________________________ ))

____________________

from-to-list)) ))

replace (car p) (cadr p) syms

symbols

14

Page 15: מבוא מורחב למדעי המחשב בשפת  Scheme

Extend-replace

> (extend-replace ‘((a e) (n m) (p a)) syms)

(p l a y – i n – e u r o p e – o r – i n – s p a i n)

(a l a y – i n – e u r o a e – o r – i n – s a a i n)

(a l a y – i m – e u r o a e – o r – i m – s a a i m)

(a l e y – i m – e u r o a e – o r – i m – s a e i m)

15

Page 16: מבוא מורחב למדעי המחשב בשפת  Scheme

Extend-replace

(define (extend-replace from-to-list symbols)

(define (scan sym)

(let ((from-to (filter

_____________________________________

_____________________________________ )))

(if (null? from-to)

___________________________

___________________________)))

(map scan symbols))

(lambda (p) (eq? (car p) sym))

from-to-list

sym

(cadr (car from-to))

16

Page 17: מבוא מורחב למדעי המחשב בשפת  Scheme

2. Data AbstractionTrie

17

Page 18: מבוא מורחב למדעי המחשב בשפת  Scheme

Trie

s a

k

t b

e s

k

t

b

e

trie1 trie2 trie3

trie4

A trie is a tree with a symbol associated with each arc. All symbols associated with arcs exiting the same node must be different.

A trie represents the set of words matching the paths from the root to the leaves (a word is simply a sequence of symbols).

{sk , t} {be} {ask , at , be} }{

Page 19: מבוא מורחב למדעי המחשב בשפת  Scheme

Available procedures(empty-trie) - The empty trie

(extend-trie symb trie1 trie2) - A constructor, returns a trie constructed from trie2, with a new arc from its root, associated with the symbol symb, connected to trie1

(isempty-trie? trie) - A predicate for an empty trie

(first-symbol trie) - A selector, returns a symbol on an arc leaving the root

(first-subtrie trie) - A selector, returns the sub-trie hanging on the arc with the symbol returned from (first-symbol trie)

(rest-trie trie) - A selector, returns the trie without the sub-trie (first-subtrie trie) and without its connecting arc

Page 20: מבוא מורחב למדעי המחשב בשפת  Scheme

word-into-trie

(define (word-into-trie word) (accumulate

word ))

(lambda (c t) (extend-trie c t empty-trie))empty-trie

Page 21: מבוא מורחב למדעי המחשב בשפת  Scheme

add-word-to-trie(define (add-word-to-trie word trie) (cond ((isempty-trie? trie) ) ((eq? (car word) (first-symbol trie))

)

(else

))

(extend-trie (car word) (add-word-to-trie (cdr word) (first-subtrie trie)) (rest-trie trie))

(extend-trie (first-symbol trie) (first-subtrie trie) (add-word-to-trie word (rest-trie trie))))

(word-into-trie word)

Page 22: מבוא מורחב למדעי המחשב בשפת  Scheme

trie-to-words(define (trie-to-words trie) (if (isempty-trie? trie) (let ((symb (first-symbol trie)) (trie1 (first-subtrie trie)) (trie2 (rest-trie trie)))

) ) )

(if (isempty-trie? trie1) (append (list (list symb)) (trie-to-words trie2)) (append (map (lambda(w) (cons symb w)) (trie-to-words trie1)) (trie-to-words trie2)))

null

Page 23: מבוא מורחב למדעי המחשב בשפת  Scheme

sub-trie word trie

(define (sub-trie word trie) (cond ((null? word) ) ((isempty-trie? trie) ) ((eq? (car word) ) ) (else )) )

_ trie ‘NO (first-symbol trie) (sub-trie (cdr word) (first-subtrie trie)) (sub-trie word (rest-trie trie))

Page 24: מבוא מורחב למדעי המחשב בשפת  Scheme

count-words-starting-with

(define (count-words-starting-with word trie) (let ((sub (sub-trie word trie)))

))

(cond ((eq? sub 'NO) 0) ((isempty-trie? sub) 1) (else (length (trie-to-words sub))))

Page 25: מבוא מורחב למדעי המחשב בשפת  Scheme

trie implementation(define empty-trie null )

(define (isempty-trie? trie) (null? trie) )

(define (extend-trie symb trie1 trie2) (cons (cons symb trie1) trie2) )

(define (first-symbol trie) (caar trie) )

(define (first-subtrie trie) (cdar trie) )

(define (rest-trie trie) (cdr trie) )

Page 26: מבוא מורחב למדעי המחשב בשפת  Scheme

Triplets

26

Page 27: מבוא מורחב למדעי המחשב בשפת  Scheme

Triplets

• Constructor– (make-node value down next)

• Selectors– (value t)– (down t)– (next t)

Page 28: מבוא מורחב למדעי המחשב בשפת  Scheme

skip

1 2 3 4 5 6 71 2 3 4 5 6

1 3 4 61 3 4 6

7

Page 29: מבוא מורחב למדעי המחשב בשפת  Scheme

skip code

(define (skip lst)

(cond ((null? lst) lst)

((= (random 2) 1)

(make-node ________________

________________

________________ ))

(else (skip ________________ ))))

(value lst)

lst

(skip (next lst))

(next lst)

Page 30: מבוא מורחב למדעי המחשב בשפת  Scheme

skip1

(define (skip1 lst) (make-node (value lst) lst (skip (next lst))))

Average length: (n+1)/2

Running Time: (n)

Page 31: מבוא מורחב למדעי המחשב בשפת  Scheme

recursive-skip1

1 2 3 4 5 6 7

1 3 4 6

1 4

1

Page 32: מבוא מורחב למדעי המחשב בשפת  Scheme

recursive-skip1 code

(define (recursive-skip1 lst)

(cond ((null? (next lst)) __________ )

(else ___________________________ )))

lst

(recursive-skip1 (skip1 lst))

Page 33: מבוא מורחב למדעי המחשב בשפת  Scheme

Accumulate-n

33

Page 34: מבוא מורחב למדעי המחשב בשפת  Scheme

Example: Accumulate-n

34

Almost same as accumulateTakes third argument as “list of lists”

Example:> (accumulate-n + 0 ‘((1 2 3) (4 5 6) (7 8 9) (10 11 12)))(22 26 30)

Page 35: מבוא מורחב למדעי המחשב בשפת  Scheme

Accumulate-n

35

(define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs)) )))