350 likes | 518 Views
מבוא מורחב למדעי המחשב בשפת 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
E N D
Outline • The special form quote • Data abstraction: Trie • Alternative list: Triplets • Accumulate-n
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)
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)
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
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
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
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
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))
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)
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)
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)
Accum-replace • (define (accum-replace from-to-list symbols) • (accumulate • (lambda(p syms) • ( ________________________________ )) • ____________________ • from-to-list)) • )) replace (car p) (cadr p) syms symbols
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)
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))
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 } {}
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 • )) (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))))
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)))
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 • Constructor • (make-node value down next) • Selectors • (value t) • (down t) • (next t)
1 3 4 6 1 3 4 6 1 2 3 4 5 6 7 skip 1 2 3 4 5 6 7
(define (skip lst) (cond ((null? lst) lst) ((= (random 2) 1) (make-node ________________ ________________ ________________ )) (else (skip ________________ )))) skip code (value lst) lst (skip (next lst)) (next lst)
(define (skip1 lst) (make-node (value lst) lst (skip (next lst)))) Average length: (n+1)/2 Running Time: (n) skip1
1 1 4 1 3 4 6 1 2 3 4 5 6 7 recursive-skip1
(define (recursive-skip1 lst) (cond ((null? (next lst)) __________ ) (else ___________________________ ))) recursive-skip1 code lst (recursive-skip1 (skip1 lst))
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)
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)) )))