Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).

Similar presentations


Presentation on theme: "מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue)."— Presentation transcript:

1 מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).

2 מבוא מורחב - שיעור 82 Formal Definition of a List A list is either –‘() -- The empty list –A pair whose cdr is a list. Note that lists are closed under the operations cons and cdr.

3 מבוא מורחב - שיעור 83 More Elaborate Lists (list 1 2 3 4) (cons (list 1 2) (list 3 4)) (list (list 1 2) (list 3 4)) 1 2 3 4 1 3 4 2 1 3 4 2

4 מבוא מורחב - שיעור 84 The Predicate Null? null? : anytype -> boolean (null? ) #t if evaluates to empty list #f otherwise (null? 2)  #f (null? (list 1))  #f (null? (cdr (list 1)))  #t (null? ’())  #t (null? null)  #t

5 מבוא מורחב - שיעור 85 The Predicate Pair? pair? : anytype -> boolean (pair? ) #t if evaluates to a pair #f otherwise. (pair? (cons 1 2))  #t (pair? (cons 1 (cons 1 2)))  #t (pair? (list 1))  #t (pair? ’())  #f (pair? 3)  #f (pair? pair?)  #f

6 מבוא מורחב - שיעור 86 The Predicate Atom? atom? : anytype -> boolean (define (atom? z) (and (not (pair? z)) (not (null? z)))) (define (square x) (* x x)) (atom? square)  #t (atom? 3)  #t (atom? (cons 1 2))  #f

7 מבוא מורחב - שיעור 87 More examples  (define digits (list 1 2 3 4 5 6 7 8 9)) (0 1 2 3 4 5 6 7 8 9)  (define digits1 (cons 0 digits))  digits1  (define l (list 0 digits))  l ? (0 (1 2 3 4 5 6 7 8 9))

8 מבוא מורחב - שיעור 88 The procedure length  (define digits (list 1 2 3 4 5 6 7 8 9))  (length digits) 9  (define l null)  (length l) 0  (define l (cons 1 l))  (length l) 1 (define (length l) (if (null? l) 0 (+ 1 (length (cdr l)))))

9 מבוא מורחב - שיעור 89 Primitives to handle lists (null? ) #t if evaluates to empty list #f otherwise (pair? ) #t if evaluates to a pair #f otherwise. (define (atom? z) (and (not (pair? z)) (not (null? z))))

10 מבוא מורחב - שיעור 810 Working with lists: some basic list manipulation

11 מבוא מורחב - שיעור 811 Cdr ing Down a List (define (list-ref lst n) (if (= n 0) (car lst) (list-ref (cdr lst) (- n 1)))) (list-ref (list 1 2 3) 0)  (list-ref (list 1 2 3) 3)  1 Error

12 מבוא מורחב - שיעור 812 Cdr ing Down a List, another example (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst)))))

13 מבוא מורחב - שיעור 813 Cons ing Up a List  (define squares (list 1 4 9 16))  (define odds (list 1 3 5 7))  (append squares odds) (1 4 9 16 1 3 5 7)  (append odds squares) (1 3 5 7 1 4 9 16) (define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1) list2))))) 1 2 3 4 list2 list1 Can’t make this pointer Change, so…

14 מבוא מורחב - שיעור 814 Append: process  (append list1 list2) (cons 1 (append (2) list2)) (cons 1 (cons 2 (append () list2))) (cons 1 (cons 2 list2)) (define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1) list2))))) 1 2 3 4 list2 list1 12 (1 2 3 4)  (define list1 (list 1 2))  (define list2 (list 3 4))

15 6/26/201515 Reverse of a list (reverse (list 1 2 3 4)) (define (reverse lst) (cond ((null? lst) lst) (else ( (reverse (cdr lst)) ))))) cons (car lst) 1 4 32 (reverse (cdr lst)) Wishful thinking… (car lst) cons

16 6/26/201516 Reverse of a list (reverse (list 1 2 3 4)) (define (reverse lst) (cond ((null? lst) lst) (else ( (reverse (cdr lst)) ))))) append (list (car lst)) (append ) (reverse (cdr lst))(list (car lst)) 4 321 4 321 Append: T(n) = c*n =  (n) Reverse: T(n) = c*(n-1) + c*(n-2)+ … + c*1 =  (n 2 )

17 מבוא מורחב - שיעור 817 Reverse (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 null (3)) (2)) (1)) (append (append (3) (2)) (1)) (append (3 2) (1)) (3 2 1) (define (reverse lst) (cond ((null? lst) lst) (else (append (reverse (cdr lst)) (list (car lst))))))) Append: T(n1) = c*n1 =  (n1) (n1 is length of list1) Reverse: T(n) = c*(n-1) + c*(n-2) … c*1 =  (n 2 )  (reverse (list 1 2 3)) (3 2 1)

18 מבוא מורחב - שיעור 818 Enumerating (integers-between 2 4) (cons 2 (integers-between 3 4))) (cons 2 (cons 3 (integers-between 4 4))) (cons 2 (cons 3 (cons 4 (integers-between 5 4)))) (cons 2 (cons 3 (cons 4 null))) (2 3 4) (define (integers-between lo hi) (cond ((> lo hi) null) (else (cons lo (integers-between (+ 1 lo) hi))))) 234

19 מבוא מורחב - שיעור 819 Enumerate Squares (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 ‘()))) (4 9 16) (define (enumerate-squares from to) (cond ((> from to) '()) (else (cons (square from) (enumerate-squares (+ 1 from) to))))) 4 9 16

20 מבוא מורחב - שיעור 820 Trees 24 68 Abstract tree: a leaf (a node that has no children, and contains data) - is a tree an internal node (a node whose children are trees) – is a tree leaf 8 6 2 4 internal node Implementation of tree: a leaf - will be the data itself an internal node – will be a list of its children

21 מבוא מורחב - שיעור 821 Count Leaves of a Tree 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 (leaf? x) (atom? x))

22 מבוא מורחב - שיעור 822 Count Leaves (define (countleaves tree) (cond ((null? tree) 0) ;base case ((leaf? tree) 1) ;base case (else ;recursive case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define my-tree (list 4 (list 5 7) 2)) 42 57

23 מבוא מורחב - שיעור 823 Countleaves (countleaves my-tree ) ==> 4 (cl (4 (5 7) 2)) + (cl 4)(cl ((5 7) 2) ) + (cl (5 7))(cl (2)) + (cl 2) (cl null) + (cl 5) (cl (7)) + (cl 7) (cl null) 1 1 10 1 0 my-tree 42 57 1 12 3 4

24 מבוא מורחב - שיעור 824 Enumerate-Leaves Goal: given a tree, produce a list of all the leaves Strategy –base case: list of empty tree is empty list –base case: list of a leaf is one element list –otherwise, recursive strategy: build a new list from a list of the leaves of the first child and a list of the leaves of the rest of the children

25 מבוא מורחב - שיעור 825 Enumerate-Leaves (define (enumerate-leaves tree) (cond ((null? tree) null) ;base case ((leaf? tree) ) ;base case (else ;recursive case ( (enumerate-leaves (car tree)) (enumerate-leaves (cdr tree))))))

26 מבוא מורחב - שיעור 826 Enumerate-Leaves (define (enumerate-leaves tree) (cond ((null? tree) null) ;base case ((leaf? tree) (list tree)) ;base case (else ;recursive case (append (enumerate-leaves (car tree)) (enumerate-leaves (cdr tree)))))) 42 57 42 57

27 מבוא מורחב - שיעור 827 Enumerate-leaves (el (4 (5 7) 2)) ap (el 4)(el ((5 7) 2) ) ap (cl (5 7))(el (2)) ap (el 2) (el nil) ap (el 5) (el (7)) ap (el 7) (el nil) (4) (5) (7)() (2) () (7) (5 7)(2) (5 7 2) (4 5 7 2)

28 מבוא מורחב - שיעור 828 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

29 מבוא מורחב - שיעור 829 Scale-tree (define (scale-tree tree factor) (cond ((null? tree) ) ;base case ((leaf? tree) ) (else ;recursive case (cons )))) (scale-tree (car tree) factor) null (* tree factor) (scale-tree (cdr tree) factor)

30 מבוא מורחב - שיעור 830 List abstraction Find common high order patterns Distill them into high order procedures Use these procedures to simplify list operations Mapping Filtering Accumulating Patterns:

31 מבוא מורחב - שיעור 831 Mapping (define (map proc lst) (if (null? lst) null (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 (integers-between 1 5) 10) ==> (10 20 30 40 50)

32 מבוא מורחב - שיעור 832 Mapping: process (define (map proc lst) (if (null? lst) null (cons (proc (car lst)) (map proc (cdr lst))))) (map square (list 1 2 3)) (cons (square 1) (map square (list 2 3))) (cons 1 (map square (list 2 3))) (cons 1 (cons (square 2) (map square (list 3)))) (cons 1 (cons 4 (map square (list 3)))) (cons 1 (cons 4 (cons (square 3) (map square null)))) (cons 1 (cons 4 (cons 9 (map square null)))) (cons 1 (cons 4 (cons 9 null))) (1 4 9)

33 מבוא מורחב - שיעור 833 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) null) ((leaf? tree) (* tree factor)) (else ;it’s a list of subtrees (map (lambda (child) (scale-tree child factor)) tree))))

34 מבוא מורחב - שיעור 834 Generalized Mapping (map + (list 1 2 3) (list 10 20 30) (list 100 200 300)) ==> (111 222 333) (map (lambda (x y) (+ x (* 2 y))) (list 1 2 3) (list 4 5 6)) ==> (9 12 15) (map … ) Returns a list in which proc is applied to the i-th elements of the lists respectively. We will see how to write such a procedure later!

35 מבוא מורחב - שיעור 835 Filtering (define (filter pred lst) (cond ((null? lst) null) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (filter odd? (integers-between 1 10)) (1 3 5 7 9)

36 מבוא מורחב - שיעור 836 Filtering: process (define (filter pred lst) (cond ((null? lst) null) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (filter odd? (list 1 2 3 4)) (cons 1 (filter odd? (list 2 3 4))) (cons 1 (filter odd? (list 3 4))) (cons 1 (cons 3 (filter odd? (list 4)))) (cons 1 (cons 3 (filter odd? null))) (cons 1 (cons 3 null)) (1 3)

37 מבוא מורחב - שיעור 837 Finding all the Primes: Sieve of Eratosthenes (a.k.a. Beta)

38 מבוא מורחב - שיעור 838.. 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)))))) (cons 2 (sieve (filter (lambda (x) (not (divisible? X 2) (list 3 4 5 …100 )))) ==> (sieve (list 2 3 4 5 … 100))

39 מבוא מורחב - שיעור 839 How sieve works Sieve takes as argument a list of numbers L and returns a list M. Take x, the first element of L and make it the first element in M. Drop all numbers divisible by x from (cdr L). Call sieve on the resulting list, to generate the rest of M. (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst))))))

40 מבוא מורחב - שיעור 840 What’s the time complexity of sieve? (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) Assume lst is (list 2 3... n). How many times is filter called?  (n) (number of primes < n) The Prime Number Theorem:  (n) = Θ (n/log n) (For large n the constants are nearly 1)

41 מבוא מורחב - שיעור 841 What’s the upper bound time complexity? (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) Filter is called Θ (n/log n) times * O(n) per call  T(n) = O( )n 2 /log n

42 מבוא מורחב - שיעור 842 What’s the Lower Bound time complexity? (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst))))))  (n/2+1..n) =  (n) -  (n/2) = Θ (n/log n) primes that are never removed from list  each such call to filter does Ω (n/log n) Work since  T(n) = Ω ( n 2 /(log n) 2 ) filter has to scan all of the list! Filter is called Θ (n/log n) times for primes p ≤ n/2

43 מבוא מורחב - שיעור 843 Another example Find the number of integers x in the range [1…100] s.t.: x * (x + 1) is divisible by 6. (length (filter _____________________________________ (map _________________________________ _________________________________)))) (lambda(n) (* n (+ n 1))) (integers-between 1 100) (lambda(n) (= 0 (remainder n 6))) Any bets on the result???? 66 (about two thirds)

44 מבוא מורחב - שיעור 844 Accumulating (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))))) Add up the elements of a list Multiply all the elements of a list

45 מבוא מורחב - שיעור 845 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)) (define (mult-all lst) (accumulate * 1 lst)) el n init op el n-1 el 1 …….. op...

46 Summary We saw operations on lists make then a natural data interface to work with Powerful operations on lists like accumulate and map make operating on lists even simpler מבוא מורחב - שיעור 846


Download ppt "מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue)."

Similar presentations


Ads by Google