Title: Introduction to computer science
1Lecture 8
2Short review from Lecture 7
3 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.
4 More Elaborate Lists
- (list 1 2 3 4)
- (cons (list 1 2) (list 3 4))
- (list (list 1 2) (list 3 4))
5 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))))
6Working with lists some basic list manipulation
7Cdring 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 (list-ref (list 1 2
3) 3) ? error
(define squares (list 1 4 9 16 25)) (list-ref
squares 3) ? 16
8 Cdring Down a List, another example
-
- (define (length lst)
- (if (null? lst)
- 0
- ( 1 (length (cdr lst)))))
9Consing Up a List
- (append squares odds)
- (1 4 9 16 25 1 3 5 7)
- (append odds squares)
- (1 3 5 7 1 4 9 16 25)
(define (append list1 list2) (cond ((null?
list1) list2) base (else (cons
(car list1) recursion (append
(cdr list1) list2)))))
10Reverse
(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 () (3)) (2)) (1))
- (append (append (3) (2)) (1))
- (append (3 2) (1))
- (3 2 1)
Append T(n) cn ?(n) Reverse T(n)
c(n-1) c(n-2) c1 ?(n2)
11Enumerating
(define (integers-between lo hi) (cond ((gt lo
hi) nil) (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 nil)))
- (list 2 3 4)
12Enumerate 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 ())))
- (list 4 9 16)
13Trees
We can view a list of possibly other lists and
atoms as a tree.
children or subtrees
root
4
leaf
2
6
8
leaf
(define tree (list 2 (list 6 8) 4)) (length tree)
? 3
14Count 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))
15Count 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))
16Countleaves
my-tree
- (countleaves my-tree )
- gt 4
-
(cl (4 (5 7) 2))
1
1
1
0
1
0
17Enumerate-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
18Enumerate-leaves
(define (enumerate-leaves tree) (cond ((null?
tree) nil) base case ((leaf? tree)
) base case (else
recursive case (
(enumerate-leaves (car tree))
(enumerate-leaves (cdr
tree))))))
19Enumerate-leaves
(define (enumerate-leaves tree) (cond ((null?
tree) nil) base case ((leaf? tree)
tree ) base case (else
recursive case (cons
(enumerate-leaves (car tree))
(enumerate-leaves (cdr
tree))))))
20Enumerate-leaves
(el (4 (5 7) 2))
(4 (5 7) 2)
((5 7) 2)
4
(5 7)
(2)
C
(7)
5
2
()
7
()
21Enumerate-Leaves
(define (enumerate-leaves tree) (cond ((null?
tree) nil) base case ((leaf?
tree) (list tree)) base case (else
recursive case (append
(enumerate-leaves (car tree))
(enumerate-leaves (cdr
tree))))))
22Enumerate-leaves
(el (4 (5 7) 2))
(4 5 7 2)
(5 7 2)
(4)
(5 7)
(2)
ap
(7)
(5)
(2)
()
(7)
()
23 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
24Scale-tree
(define (scale-tree tree factor) (cond ((null?
tree) ()) base case ((leaf?
tree) ( tree factor)) (else
recursive case (cons
(scale-tree (cdr tree) factor )))))
(scale-tree (car tree) factor)
25List abstraction
- Find common high order patterns
- Distill them into high order procedures
- Use these procedures to simplify list operations
Patterns
- Mapping
- Filtering
- Accumulating
26Mapping
(define (map proc lst) (if (null? lst)
nil (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)
27Alternative 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 its a list of
subtrees (map (lambda (child)
(scale-tree child factor))
tree))))
28Generalized 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)
29Filtering
- (define (filter pred lst)
- (cond ((null? lst) nil)
- ((pred (car lst))
- (cons (car lst)
- (filter pred (cdr lst))))
- (else (filter pred (cdr lst)))))
(filter odd? (square-list (integers-between 1
10))) (1 9 25 49 81)
30Finding all the Primes
31.. 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 (divisable? X 2) (list 3 4 5 100 ))))