Presentation is loading. Please wait.

Presentation is loading. Please wait.

List and list operations (continue).

Similar presentations


Presentation on theme: "List and list operations (continue)."— Presentation transcript:

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

2 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. מבוא מורחב - שיעור 8

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

4 The Predicate Null? (null? <z>) null? : anytype -> boolean
#t if <z> evaluates to empty list #f otherwise (null? 2)  #f (null? (list 1))  #f (null? (cdr (list 1)))  #t (null? ’())  #t (null? null)  #t The beauty of list is that cdr allows us to easily move down a list, and we have a simple test to find out when we have reached the end, we get to the empty list, that is, to a list whose value is null. How do we check that we have reached the end, scheme supplies us with the predicate null? מבוא מורחב - שיעור 8

5 The Predicate 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 (pair? 3)  #f (pair? pair?)  #f מבוא מורחב - שיעור 8

6 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 Not a primitive procedure מבוא מורחב - שיעור 8

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

8 Cdring 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)  1 Error (list-ref (list 1 2 3) 3)  מבוא מורחב - שיעור 8

9 Cdring Down a List, another example
(define (length lst) (if (null? lst) (+ 1 (length (cdr lst))))) מבוא מורחב - שיעור 8

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

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

12 Reverse of a list cons (define (reverse lst) (cond ((null? lst) lst)
(else ( (reverse (cdr lst)) ))))) cons (car lst) (reverse (list )) cons 1 4 3 2 (reverse (cdr lst)) Wishful thinking… (car lst) 4/7/2019

13 Reverse of a list (append ) (define (reverse lst)
(cond ((null? lst) lst) (else ( (reverse (cdr lst)) ))))) append (list (car lst)) (reverse (list )) (reverse (cdr lst)) (list (car lst)) 4 3 2 1 (append ) Append: T(n) = c*n = (n) Reverse: T(n) = c*(n-1) + c*(n-2)+ … + c*1 = (n2) 4 3 2 1 4/7/2019

14 Enumerating (define (integers-between lo hi) (cond ((> lo hi) null)
(else (cons lo (integers-between (+ 1 lo) hi))))) (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) 2 3 4 מבוא מורחב - שיעור 8

15 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 ‘()))) (4 9 16) 4 9 16 מבוא מורחב - שיעור 8

16 Trees 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 2 4 6 8 מבוא מורחב - שיעור 8

17 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)) מבוא מורחב - שיעור 8

18 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)) 4 2 5 7 מבוא מורחב - שיעור 8

19 Countleaves my-tree (countleaves my-tree ) ==> 4 4 4 2 5 7
(cl (4 (5 7) 2)) 4 + (cl 4) (cl ((5 7) 2) ) 3 1 + (cl (5 7)) (cl (2)) 2 1 + (cl 5) (cl (7)) + (cl 2) (cl null) 1 + (cl 7) (cl null) 1 1 1 מבוא מורחב - שיעור 8

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

21 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)))))) מבוא מורחב - שיעור 8

22 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)))))) 4 2 5 7 4 2 5 7 מבוא מורחב - שיעור 8

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

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

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

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

27 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) ==> ( ) מבוא מורחב - שיעור 8

28 Mapping: process (map square (list 1 2 3))
(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) מבוא מורחב - שיעור 8

29 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)))) מבוא מורחב - שיעור 8

30 We will see how to write such a procedure later!
Generalized Mapping (map <proc> <list1>…<listn>) Returns a list in which proc is applied to the i-th elements of the lists respectively. (map + (list 1 2 3) (list ) (list )) ==> ( ) (map (lambda (x y) (+ x (* 2 y))) (list 1 2 3) (list 4 5 6)) ==> ( ) We will see how to write such a procedure later! מבוא מורחב - שיעור 8

31 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)) ( ) מבוא מורחב - שיעור 8

32 Filtering: process (filter odd? (list 1 2 3 4))
(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 )) (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) מבוא מורחב - שיעור 8

33 Finding all the Primes: Sieve of Eratosthenes (a.k.a. Beta)
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 5 XX X 3 XX X 2 XX 7 מבוא מורחב - שיעור 8

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

35 How sieve works (define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) 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. מבוא מורחב - שיעור 8

36 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 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) מבוא מורחב - שיעור 8

37 What’s the time complexity of sieve? (cont’)
(define (sieve lst) (if (null? lst) ‘() (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst))))))  T(n) = O( ) n2/log n Filter is called Θ(n/log n) times Filter is called Θ(n/log n) times for primes p ≤ n/2 (n/2+1..n) = (n) - (n/2) = Θ(n/log n)  Each such call to filter does Ω(n/log n) work  T(n) = Ω( n2 /(log n)2 ) The problem is that filter has to scan all of the list! מבוא מורחב - שיעור 8

38 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) (= 0 (remainder n 6))) (lambda(n) (* n (+ n 1))) (integers-between 1 100) 66 (about two thirds) Any bets on the result???? מבוא מורחב - שיעור 8

39 Accumulating Add up the elements of a list
(define (add-up lst) (if (null? lst) (+ (car lst) (add-up (cdr lst))))) Multiply all the elements of a list (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))))) מבוא מורחב - שיעור 8

40 Accumulating (cont.) el1 eln-1 eln init …….. op ...
(define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) eln init op eln-1 el1 …….. ... (define (add-up lst) (accumulate + 0 lst)) (define (mult-all lst) (accumulate * 1 lst)) מבוא מורחב - שיעור 8


Download ppt "List and list operations (continue)."

Similar presentations


Ads by Google