Title: List and list operations continue'
1Lecture 8
- List and list operations (continue).
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.
3 More Elaborate Lists
- (list 1 2 3 4)
- (cons (list 1 2) (list 3 4))
- (list (list 1 2) (list 3 4))
4 The Predicate Null?
- null? anytype -gt boolean
- (null? ltzgt)
- t if ltzgt evaluates to empty list
- f otherwise
- (null? 2) ? f
- (null? (list 1)) ? f
- (null? (cdr (list 1))) ? t
- (null? ()) ? t
- (null? null) ? t
5 The Predicate Pair?
- pair? anytype -gt boolean
- (pair? ltzgt) t if ltzgt 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 The Predicate Atom?
- atom? anytype -gt 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
7More examples
- (define digits (list 1 2 3 4 5 6 7 8 9))
(0 1 2 3 4 5 6 7 8 9)
8The procedure length
- (define digits (list 1 2 3 4 5 6 7 8 9))
- (length digits)
- (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 Primitives to handle lists
- (null? ltzgt)
- t if ltzgt evaluates to empty list
- f otherwise
- (pair? ltzgt) t if ltzgt evaluates to a pair
f otherwise. - (define (atom? z)
- (and (not (pair? z))
- (not (null? z))))
10Working with lists some basic list manipulation
11Cdring Down a List
- (define (list-ref lst n)
- (if ( n 0)
- (car lst)
- (list-ref (cdr lst) (- n 1))))
1
(list-ref (list 1 2 3) 0) ?
Error
(list-ref (list 1 2 3) 3) ?
12 Cdring Down a List, another example
-
- (define (length lst)
- (if (null? lst)
- 0
- ( 1 (length (cdr lst)))))
13Consing 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)))))
14Append process
(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))
- (append list1 list2)
- (cons 1 (append (2) list2))
- (cons 1 (cons 2 (append () list2)))
- (cons 1 (cons 2 list2))
-
(1 2 3 4)
15Reverse of a list
(define (reverse lst) (cond ((null? lst)
lst) (else ( (reverse (cdr lst))
)))))
cons
(car lst)
(reverse (list 1 2 3 4))
16Reverse of a list
(define (reverse lst) (cond ((null? lst)
lst) (else ( (reverse (cdr lst))
)))))
append
(list (car lst))
(reverse (list 1 2 3 4))
(append )
Append T(n) cn ?(n) Reverse T(n)
c(n-1) c(n-2) c1 ?(n2)
17Reverse
- (reverse (list 1 2 3))
- (3 2 1)
(define (reverse lst) (cond ((null? lst)
lst) (else (append (reverse (cdr lst))
(list (car lst)))))))
- (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)
Append T(n1) cn1 ?(n1) (n1 is length of
list1) Reverse T(n) c(n-1) c(n-2) c1
?(n2)
18Enumerating
(define (integers-between lo hi) (cond ((gt 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)
19Enumerate Squares
(define (enumerate-squares from to) (cond ((gt
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)
20Trees
- 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
- Implementation of tree
- a leaf - will be the data itself
- an internal node will be a list of its
children
21Count 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))
22Count 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))
23Countleaves
my-tree
- (countleaves my-tree )
- gt 4
-
(cl (4 (5 7) 2))
4
3
1
1
2
1
1
1
0
1
0
24Enumerate-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
25Enumerate-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))))))
26Enumerate-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))))))
27Enumerate-leaves
(el (4 (5 7) 2))
(4 5 7 2)
(5 7 2)
(4)
(5 7)
(2)
ap
(7)
(5)
(2)
()
(7)
()
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
29Scale-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)
30List abstraction
- Find common high order patterns
- Distill them into high order procedures
- Use these procedures to simplify list operations
Patterns
- Mapping
- Filtering
- Accumulating
31Mapping
(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) gt (10 20
30 40 50)
32Mapping 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)
33Alternative 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 its a list of
subtrees (map (lambda (child)
(scale-tree child factor))
tree))))
34Generalized Mapping
(map ltprocgt ltlist1gtltlistngt) Returns a list in
which proc is applied to the i-th elements of the
lists respectively.
(map (list 1 2 3) (list 10 20 30) (list 100 200
300)) gt (111 222 333)
(map (lambda (x y) ( x ( 2 y))) (list 1 2
3) (list 4 5 6)) gt (9 12 15)
We will see how to write such a procedure later!
35Filtering
- (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)
36Filtering 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)
37Finding all the Primes Sieve of Eratosthenes
(a.k.a. Beta)
38.. And heres how to do it!
- (define (sieve lst)
- (if (null? lst)
- ()
- (cons (car lst)
- (sieve (filter
- (lambda (x)
- (not (divisible? x (car
lst)))) - (cdr lst))))))
gt (sieve (list 2 3 4 5 100))
(cons 2 (sieve (filter (lambda (x)
(not (divisible? X 2) (list 3 4 5 100 ))))
39How 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.
40Whats 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 lt n)
41Whats 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))))))
Filter is called T(n/log n) times
Filter is called T(n/log n) times for primes p
n/2
?(n/21..n) ?(n) - ?(n/2) T(n/log n)
? Each such call to filter does O(n/log n)
work
? T(n) O( n2 /(log n)2 )
The problem is that filter has to scan all of the
list!
42Another example
- Find the number of integers x in the range
1100 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)
Any bets on the result????
43Accumulating
Add up the elements of a list
(define (add-up lst) (if (null? lst) 0
( (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)))))
44Accumulating (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))