Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.

Similar presentations


Presentation on theme: "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n."— Presentation transcript:

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

2 Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n

3 1. The Special Form quote 3

4 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

5 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

6 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

7 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

8 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

9  (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

10 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

11 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

12 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

13 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

14 Accum-replace (define (accum-replace from-to-list symbols) (accumulate (lambda(p syms) ( ________________________________ )) ____________________ from-to-list)) )) replace (car p) (cadr p) syms symbols 14

15 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

16 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

17 2. Data Abstraction Trie 17

18 Trie s a k tb 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 }{}

19 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

20 word-into-trie (define (word-into-trie word) (accumulate word ) (lambda (c t) (extend-trie c t empty-trie)) empty-trie

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

22 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

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

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

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

26 Triplets 26

27 Triplets Constructor –(make-node value down next) Selectors –(value t) –(down t) –(next t)

28 skip 1234567 123456 1346 1 346 7

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

30 skip1 (define (skip1 lst) (make-node (value lst) lst (skip (next lst)))) Average length: (n+1)/2 Running Time:  (n)

31 recursive-skip1 1234567 1346 14 1

32 recursive-skip1 code (define (recursive-skip1 lst) (cond ((null? (next lst)) __________ ) (else ___________________________ ))) lst (recursive-skip1 (skip1 lst))

33 Accumulate-n 33

34 Example: Accumulate-n 34 Almost same as accumulate Takes 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)

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


Download ppt "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n."

Similar presentations


Ads by Google