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

42
ררררר- רררר ררררר8 1 Lecture 8 Lists and list operations (continue).
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    1

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

8מבוא מורחב - שיעור 1

Lecture 8

Lists 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 42

8מבוא מורחב - שיעור 4

The Predicate 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

(null? null) #t

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

8מבוא מורחב - שיעור 10

Working with lists: some basic list manipulation

8מבוא מורחב - שיעור 11

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)

(list-ref (list 1 2 3) 3)

1

Error

8מבוא מורחב - שיעור 12

Cdring Down a List, another example

(define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst)))))

8מבוא מורחב - שיעור 13

Consing 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

list2list1

Can’t make this pointer Change, so…

8מבוא מורחב - שיעור 14

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

list2list1

1 2

(1 2 3 4)

(define list1 (list 1 2))(define list2 (list 3 4))

04/18/23 15

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 3 2

(reverse (cdr lst))

Wishful thinking…

(car lst)cons

04/18/23 16

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 3 2 1

4 3 2 1

Append: T(n) = c*n = (n)

Reverse: T(n) = c*(n-1) + c*(n-2)+

… + c*1 = (n2)

8מבוא מורחב - שיעור 18

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

2 3 4

8מבוא מורחב - שיעור 19

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

8מבוא מורחב - שיעור 20

Trees

2 4

6 8

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

leaf

86

2 4

internal node

internal node

Implementation of tree:

• a leaf - will be the data itself

• an internal node – will be a list of its children

8מבוא מורחב - שיעור 21

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

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

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

1 0

1 0

my-tree

4 2

5 7

1

12

3

4

8מבוא מורחב - שיעור 24

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

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

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

4 2

5 7

8מבוא מורחב - שיעור 27

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)

8מבוא מורחב - שיעור 28

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

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)

8מבוא מורחב - שיעור 30

List abstraction

• Find common high order patterns• Distill them into high order procedures• Use these procedures to simplify list operations

• Mapping• Filtering• Accumulating

Patterns:

8מבוא מורחב - שיעור 31

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)

8מבוא מורחב - שיעור 32

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)

8מבוא מורחב - שיעור 33

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

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 <proc> <list1>…<listn>)

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!

8מבוא מורחב - שיעור 35

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)

8מבוא מורחב - שיעור 36

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)

8מבוא מורחב - שיעור 37

Finding all the Primes: Sieve of Eratosthenes (a.k.a. Beta)

XX XX

XX

XX

XX XX

XX

XX XX

XX

XX XX

XX

7

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XX XX XX XX XX

XXXX X 2

XX XX XX

XX XX XX XX

XX XX XX

XX XX XX

XX XX XX XX

XX XX XX

XX XX XX

XX XX XX XX

XX XX XX

X X 3

100999897969594939291

90898887868584838281

80797877767574737271

60696867666564636261

60595857565554535251

50494847464544434241

40393837363534333231

30292827262524232221

20191817161514131211

1098765432

XX XX

XX XX

XX XX

XX XX

XX XX

XX XX

XX XX

XX XX

XX XX

XX 5

8מבוא מורחב - שיעור 38

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

8מבוא מורחב - שיעור 39

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

8מבוא מורחב - שיעור 40

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)

8מבוא מורחב - שיעור 41

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( )n2/log n

8מבוא מורחב - שיעור 42

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) = Ω( n2 /(log n)2 )

filter has to scan all of the list!

Filter is called Θ(n/log n) times for primes p ≤ n/2

8מבוא מורחב - שיעור 43

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)

8מבוא מורחב - שיעור 44

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

8מבוא מורחב - שיעור 45

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

eln init

op

eln-1 el1 ……..

opop

op...

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

8מבוא מורחב - שיעור 46