1 / 31

Lecture #7

Lecture #7. 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.2. How can we implement pairs ?. ( define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or 1 -- CONS" m)))))).

Download Presentation

Lecture #7

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. Lecture #7 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.2 מבוא מורחב

  2. How can we implement pairs ? (define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or 1 -- CONS" m)))))) (define (car z) (z 0)) (define (cdr z) (z 1)) מבוא מורחב

  3. The pair (cons 6 9) The pair constructed by (cons 6 9) is the procedure: (lambda (m) (cond ((= m 0) 6) ((= m 1) 9) (else (error “Argument not 0 or 1 -- CONS” m)))) מבוא מורחב

  4. <x1> <x2> <xn> Lists (list <x1> <x2> ... <xn>) Same as (cons <x1> (cons <x2> ( … (cons <xn> nil)))) מבוא מורחב

  5. lists A list is either • ‘() (The empty list) • A pair whose cdr is a list. Note that lists are closed under operations of consand cdr. מבוא מורחב

  6. Null? null? : anytype -> boolean (null? <z>) #t if <z> evaluates to empty list #f otherwise (null? 2)  #f (null? (list 1))  #f (null? (cdr (list 1)))  #t (null? ‘())  #t מבוא מורחב

  7. pair? pair? : anytype -> boolean (pair? <z>) #tif <z> evaluates to a pair #f otherwise. (pair? (cons 1 2))  #t (pair? (cons 1 (cons 1 2)))  #t (pair? (list 1))  #t (pair? ‘())  #f מבוא מורחב

  8. atom? atom? : anytype -> boolean (define (atom? z) (and (not (pair? z)) (not (null? z)))) (define (sqrt x) (* x )) (atom? sqrt)  #t מבוא מורחב

  9. caddr (define one-to-four (list 1 2 3 4)) one-to-four ==> (1 2 3 4) (1 2 3 4) ==> error (car one-to-four) ==> 1 2 (car (cdr one-to-four)) ==> (cadr one-to-four) ==> 2 3 (caddr one-to-four) ==> מבוא מורחב

  10. (enumerate-primes 2 5) (cons 2 (enumerate-primes (+ 1 2) 4))) (cons 2 (cons 3 (enumerate-primes 4 5))) (cons 2 (cons 3 (enumerate-primes 5 5))) (cons 2 (cons 3 (cons 5 (enumerate-primes 6 5)))) (cons 2 (cons 3 (cons 5 ‘()))) (list 2 3 5) 2 3 5 Common Pattern #1: cons’ing up a list (define (enumerate-primes from to) (cond ((> from to) '()) ((prime? from) (cons from (enumerate-primes (+ 1 from) to))) (else (enumerate-primes (+ 1 from) to)))) מבוא מורחב

  11. (enumerate-squares 2 4) (cons 4 (enumerate-squares 3 4))) (cons 4 (cons 9 (enumerate-squares 4 4))) (cons 4 (cons 9 (cons 16 (enumerate-squares 5 4)))) (cons 4 (cons 9 (cons 16 ‘()))) (list 4 9 16) 4 9 16 Common Pattern #1: enumerate-squares (define (enumerate-squares from to) (cond ((> from to) '()) (else (cons (square from) (enumerate-squares (+ 1 from) to))))) מבוא מורחב

  12. (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst))))) Common Pattern #2: cdr’ing down a list (length (list 1 2 3))  3 (length (enumerate-squares 1 100))  100 (length (enumerate-primes 1 1000))  169 (length (enumerate-primes 1001 2000))  135 (length (enumerate-primes 100001 101000))  81 (length (enumerate-primes 10000001 10001000))  61 מבוא מורחב

  13. Length – iterative version (define (length lst) (define (length-iter temp count) (if (null? temp) count (length-iter (cdr temp) (+ 1 count)))) (length-iter lst 0)) מבוא מורחב

  14. (define (list-ref lst n) ; find n’th element (if (= n 1) (car lst) (list-ref (cdr lst) (- n 1)))) Another example: cdr’ing down a list (list-ref (list 1 2 3) 1)  1 (list-ref (list 1 2 3) 4)  car: expects argument of type <pair>; given () (define list-of-primes (enumerate-primes 2 10000)) (list-ref list-of-primes 57)  269 מבוא מורחב

  15. (define (append list1 list2) (cond ((null? list1) list2); base (else (cons (car list1); recursion (append (cdr list1)list2))))) Append (append (list 1 2) (list 3 4)) (cons 1 (append (2) (3 4))) (cons 1 (cons 2 (append () (3 4)))) (cons 1 (cons 2 (3 4))) (1 2 3 4) Time complexity: T(n) = (n) car,cdr,cons operations.

  16. (reverse (list 1 2 3)) (append (reverse (2 3)) (1)) (append (append (reverse (3)) (2)) (1)) (append (append (append (reverse ()) (3)) (2)) (1)) (append (append (append () (3)) (2)) (1)) (append (append (3) (2)) (1)) (append (3 2) (1)) (3 2 1) Reverse (define (reverse lst) (cond ((null? lst) lst) (else (append (reverse (cdr lst)) (list (car lst))))))) (reverse (list 1 2 3 4)) ==> (4 3 2 1) Append: T(n) = c*n = (n) Reverse: T(n) = c*(n-1) + c*(n-2) … c*1 = (n2)

  17. Square-list, double-list, (define (square-list lst) (if (null? lst) ‘() (cons (square (car lst)) (square-list (cdr lst))))) (define (double-list lst) (if (null? lst) ‘() (cons (* 2 (car lst)) (double-list (cdr lst))))) מבוא מורחב

  18. High order procedures to handle lists: map (define (map proc lst) (if (null? lst) ‘() (cons (proc (car lst)) (map proc (cdr lst))))) (define (square-list lst) (map square lst)) (define (scale-list lst c) (map (lambda (x) (* c x)) lst)) (scale-list (list 1 2 3 5) 10) ==> (10 20 30 50) מבוא מורחב

  19. Pick odd elements out of a list (define (odd-elements lst) (cond ((null? lst) ‘()) ((odd? (car lst)) (cons (car lst) (odd-elements (cdr lst)))) (else (odd-elements (cdr lst))))) (enumerate-squares 2 6)  (4 9 16 25 36) (odd-elements (enumerate-squares 2 6))  (9 25) מבוא מורחב

  20. (define (filter pred lst) (cond ((null? lst) ‘()) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) Filtering a List (filter) (define list-of-squares (enumerate-squares 1 10)) (filter odd? list-of-squares) (1 9 25 49 81) מבוא מורחב

  21. Accumulating Results (accumulate) (define (add-up lst) (if (null? lst) 0 (+ (car lst) (add-up (cdr lst))))) (define (mult-all lst) (if (null? lst) 1 (* (car lst) (mult-all (cdr lst))))) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) מבוא מורחב

  22. eln eln-1 init el1 …….. op op op ... op Accumulating (cont.) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) (define (add-up lst) (accumulate + 0 lst)) מבוא מורחב

  23. Length and append as accumulation (define (length lst) (accumulate (lambda (x y) (+ 1 y))) 0 lst)) (define (append lst1 lst2) (accumulate cons lst2 lst1) מבוא מורחב

  24. 2 3 4 5 6 7 8 9 10 2 X X 7 X XX 11 12 13 14 15 16 17 18 19 20 XX XX XX XX XX XX 21 22 23 24 25 26 27 28 29 30 XX XX XX XX XX XX XX 3 5 X X XX 31 32 33 34 35 36 37 38 39 40 XX XX XX XX XX XX XX XX XX XX XX 41 42 43 44 45 46 47 48 49 50 XX XX XX XX XX XX XX XX XX XX XX XX XX 51 52 53 54 55 56 57 58 59 60 XX XX XX XX XX XX XX XX XX XX XX 61 62 63 64 65 66 67 68 69 60 XX XX XX XX XX XX XX XX XX XX XX XX 71 72 73 74 75 76 77 78 79 80 XX XX XX XX XX XX XX XX XX XX XX XX 81 82 83 84 85 86 87 88 89 90 XX XX XX XX XX XX XX XX XX XX XX XX XX 91 92 93 94 95 96 97 98 99 100 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX Finding all the primes מבוא מורחב

  25. .. And here’s how to do it! (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) מבוא מורחב

  26. 6 children or subtrees root 4 2 2 6 8 4 8 Trees We can view a list of possibly other lists and atoms as a tree. (define tree (list 2 (list 6 8) 4)) (length tree)  3 מבוא מורחב

  27. countleaves • Strategy • base case: count of an empty tree is 0 • base case: count of a leaf is 1 • recursive strategy: the count of a tree is the sum of the countleaves of each child in the tree. • Implementation: (define (countleaves tree) (cond ((null? tree) 0) ;base case ((leaf? tree) 1) ;base case (else ;recursive case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define (leaf? x) (not (pair? x))) מבוא מורחב

  28. 5 my-tree 4 (cl 4) (cl ((5 7) 2) ) + + (cl (5 7)) (cl (2)) + + (cl (7)) (cl 5) (cl 2) (cl nil) + 7 2 (cl 7) (cl nil) countleaves – example (define my-tree (list 4 (list 5 7) 2)) (countleaves my-tree) (countleaves (4 (5 7) 2)) (+ (countleaves 4) (countleaves ((5 7) 2))) ==> 4 (cl (4 (5 7) 2)) 1 1 1 0 1 0

  29. Your Turn: scale-tree • Goal: given a tree, produce a new tree with all the leaves scaled • Strategy • base case: scale of empty tree is empty tree • base case: scale of a leaf is product • otherwise, recursive strategy: build a new tree from a scaled version of the first child and a scaled version of the rest of children מבוא מורחב

  30. Scale-tree (define (scale-tree tree factor) (cond ((null? tree) nil) ;base case ((leaf? tree) (* tree factor)) (else ;recursive case (cons (scale-tree (cdr tree) factor ))))) (scale-tree (car tree) factor) מבוא מורחב

  31. Alternative scale-tree • Strategy • base case: scale of empty tree is empty tree • base case: scale of a leaf is product • otherwise: a tree is a list of subtrees and use map. (define (scale-tree tree factor) (cond ((null? tree) nil) ((leaf? tree) (* tree factor)) (else ;it’s a list of subtrees (map (lambda (child) (scale-tree child factor)) tree))))

More Related