Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.

Similar presentations


Presentation on theme: "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2."— Presentation transcript:

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

2 Outline More list examples Symbols 2

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

4 4 skip 1234567 123456 1346 1 346 7

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

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

7 7 recursive-skip1 1234567 1346 14 1

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

9 9 make-star g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1

10 10 make-star (define (make-star g) (define (g* x) (if (<= x 1) 0 (+ 1 (g* (g x))))) g* )

11 11 log (log 4) ==> 2 (log 1) ==> 0 (log 5) ==> 3 (define log (make-star ____________________ ) (lambda (x) (/ x 2))

12 12 compose-stars Input: a list of functions –(f g h) Output: a composite function: –fgh*(x) := f*(g*(h*(x))) Idea: –Make a list of star functions –Compose them together

13 13 compose-stars (define (compose-stars lst) (accumulate _______________________________ (map __________________________ __________________________ ))) compose (lambda (x) x) make-star lst

14 Example: Horner Rule 14 Evaluating a polynomial in x at a given value of x Algorithm :Horner's rule

15 Horner Rule: Cont’d 15 (define (horner-eval x coeff-sequence) (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms))) 0 coeff-sequence)) P(x)=1+2*x+2*x*x P(3)=25 > (horner-eval 3 '(1 2 2))

16 The Special Form quote 16

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

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

19 19 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. Therefore: (eq? ‘a ‘a)  #t But, (eq? ‘(a b) ‘(a b))  #f

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

21 eq? vs. equal? (symbols) > (eq? ‘a ‘a) > (equal? ‘a ‘a) > (define x ‘a) > (define y ‘a) > (eq? x y) > (equal? x y) 21

22 (eq? (list 1 2 3) (list 1 2 3)) (equal? (list 1 2 3) (list 1 2 3)) (equal? (list (list (list 1) 2) (list 1)) (list (list (list 1) 2) (list 1))) (define x (list 1 2 3)) (define y (list 1 2 3)) (eq? x y) (define z y) (eq? z y) (eq? x z) eq? vs. equal? (symbols) 22

23 Example: Accumulate-n 23 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)

24 Accumulate-n: Cont’d 24 (define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs)) )))

25 25 Example 1: memq (define (memq item lst) (cond ((null? lst) #f) ((eq? item (car lst)) lst) (else (memq item (cdr lst))))) (memq 'a '(b a b c)) => (memq 'a '((a b) b c)) => (memq 'a '((a b) b a (c a) d)) => (a b c) #f (a (c a) d) If item does not appear in lst, returns false. Otherwise – returns the sublist beginning with item.

26 26 Example 2: rember (define (rember item lst) (cond ((null? lst) '()) ((eq? item (car lst))(cdr lst)) (else (cons (car lst) (rember item (cdr lst))))) ) (rember 'a '(d a b c a)) => (rember 'b '(a b c)) => (rember 'a '(b c d)) => (d b c a) (a c) (b c d) Removes the first occurrence of item from lst.

27 27 Example 3: rember* (define (rember* item lst) (cond ((null? lst) '()) ((atom? (car lst)) (if (eq? (car lst) item) (rember* item (cdr lst)) (cons (car lst) (rember* item (cdr lst))))) (else (cons (rember* item (car lst)) (rember* item (cdr lst)))))) (rember* 'a '(a b)) => (rember* 'a '(a (b a) c (a)) => (b) ((b) c ()) Removes all occurrences of item from lst, at all levels

28 Quine 28 ((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x)))) A quine is a computer program that produces its own source code as its only output.

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

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

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

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

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

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

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


Download ppt "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2."

Similar presentations


Ads by Google