Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #7 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.2 מבוא מורחב.

Similar presentations


Presentation on theme: "Lecture #7 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.2 מבוא מורחב."— Presentation transcript:

1 Lecture #7 90-92, 99-103, 2.2.1 , 2.2.1 , 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 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 … Lists (list <x1> <x2> ... <xn>) Same as
(cons <x1> (cons <x2> ( … (cons <xn> nil)))) <x1> <x2> <xn> מבוא מורחב

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

6 Null? (null? (list 1))  #f (null? (cdr (list 1)))  #t
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>) #t if <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 Not a primitive procedure מבוא מורחב

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

10 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)))) (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 מבוא מורחב

11 Common Pattern #1: enumerate-squares
(define (enumerate-squares from to) (cond ((> from to) '()) (else (cons (square from) (enumerate-squares (+ 1 from) to))))) (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 מבוא מורחב

12 Common Pattern #2: cdr’ing down a list
(define (length lst) (if (null? lst) (+ 1 (length (cdr lst))))) (length (list 1 2 3))  3 (length (enumerate-squares 1 100))  100 (length (enumerate-primes ))  169 (length (enumerate-primes ))  135 (length (enumerate-primes ))  81 (length (enumerate-primes ))  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 Another example: cdr’ing down a list
(define (list-ref lst n) ; find n’th element (if (= n 1) (car lst) (list-ref (cdr lst) (- n 1)))) (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 )) (list-ref list-of-primes 57)  269 מבוא מורחב

15 Append (define (append list1 list2) (cond ((null? list1) list2) ; base
(else (cons (car list1) ; recursion (append (cdr list1)list2))))) (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))) ( ) Time complexity: T(n) = (n) car,cdr,cons operations.

16 Reverse (define (reverse lst) (cond ((null? lst) lst)
(else (append (reverse (cdr lst)) (list (car lst))))))) (reverse (list )) ==> ( ) (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) 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 ) 10) ==> ( ) מבוא מורחב

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)  ( ) (odd-elements (enumerate-squares 2 6))  (9 25) מבוא מורחב

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

21 Accumulating Results (accumulate)
(define (add-up lst) (if (null? lst) (+ (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 Accumulating (cont.) (define (accumulate op init lst) (if (null? lst)
(op (car lst) (accumulate op init (cdr lst))))) eln init op eln-1 el1 …….. ... (define (add-up lst) (accumulate + 0 lst)) מבוא מורחב

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

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

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 Trees We can view a list of possibly other lists and atoms as a tree.
children or subtrees 2 6 8 4 root 2 4 6 8 (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 countleaves – example my-tree (define my-tree (list 4 (list 5 7) 2))
(countleaves my-tree) (countleaves (4 (5 7) 2) ) (+ (countleaves 4) (countleaves ((5 7) 2) )) ==> 4 4 2 5 7 my-tree (cl (4 (5 7) 2)) + (cl 4) (cl ((5 7) 2) ) 1 + (cl (5 7)) (cl (2)) + (cl 5) (cl (7)) + (cl 2) (cl nil) + (cl 7) (cl nil) 1 1 1

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


Download ppt "Lecture #7 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.2 מבוא מורחב."

Similar presentations


Ads by Google