מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form...

Post on 18-Jan-2018

226 views 0 download

description

1. The Special Form quote 3

Transcript of מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form...

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

8תרגול

Outline

1. The special form quote2. Data abstraction: Trie3. Alternative list: Triplets4. Accumulate-n

1. The Special Formquote

3

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

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

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

The predicate equal?

A primitive procedure that tests if the pointers represent identical objects

1. For symbols, eq? and equal? are equivalent2. 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

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

(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

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

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

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

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

Accum-replace

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

(accumulate

(lambda(p syms)

( ________________________________ ))

____________________

from-to-list)) ))

replace (car p) (cadr p) syms

symbols

14

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

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

2. Data AbstractionTrie

17

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} }{

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

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

word ))

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

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)

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

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

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

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

Triplets

26

Triplets

• Constructor– (make-node value down next)

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

skip

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

1 3 4 61 3 4 6

7

skip code

(define (skip lst) (cond ((null? lst) lst) ((= (random 2) 1) (make-node ________________ ________________ ________________ )) (else (skip ________________ ))))

(value lst)

lst

(skip (next lst))

(next lst)

skip1

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

Average length: (n+1)/2Running Time: (n)

recursive-skip1

1 2 3 4 5 6 7

1 3 4 6

1 4

1

recursive-skip1 code

(define (recursive-skip1 lst)

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

(else ___________________________ )))

lst

(recursive-skip1 (skip1 lst))

Accumulate-n

33

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)

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