1 / 35

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

מבוא מורחב למדעי המחשב בשפת 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

keira
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 1. The Special Formquote

  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)

  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)

  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

  7. The predicate equal? A primitive procedure that tests if the pointers represent identical objects • For symbols, eq? and equal? are equivalent • If two pointers are eq?, they are surely equal? • 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

  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

  9. eq? vs. equal? (symbols) • (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

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

  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)

  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)

  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 ee i m)

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

  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 pa i n) (a l a y – i n – e u r o a e – o r – i n – s aa 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)

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

  17. 2. Data AbstractionTrie

  18. b a t b s trie4 t e s e k k trie2 trie1 trie3 Trie 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 • )) (word-into-trie word) (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))))

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

  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

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

  28. 1 3 4 6 1 3 4 6 1 2 3 4 5 6 7 skip 1 2 3 4 5 6 7

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

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

  31. 1 1 4 1 3 4 6 1 2 3 4 5 6 7 recursive-skip1

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

  33. Accumulate-n

  34. Example: Accumulate-n 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 (define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs)) )))

More Related