Prove by induction
Solution is missing...
b) 40
c) (4)
d) stack overflow due to infinite loop
e) 9
pairs(n) = pairs(n-1) + (n-1)
= pairs(n-2) + (n-2) + (n-1)
... = pairs(1) + 1 + 2 + ... + (n-2) + (n-1)
pairs(1) = 0, pairs(n) = (n(n-1))/2
for (l <list>) of length n
- pair-with-list is O(n)
- length (pair-with-list x l) => n where x is any object
T(n) = T(n-1) + O(n-1) + O(n-1) + C
^all-pairs ^append ^pair-with-list
recursive call (length (tail l)) => n-1
= T(n-2) + 2O(n-2) + 2O(n-1) + C
... = T(1) + 2O(1) + 2O(2) + ... + 2O(n-1) + C T(1) = C
= 2O((n(n-1))/2) = O(n2)
(define (copy-list <function>)
(method ((l <list>))
(map (method (x) x) l)))
(define (deriv-list <function>)
(method ((l <list>) (acc <number>))
(map (method (x) (deriv x acc)) l)))
(define (map <function>)
(method ((f <function>) (l <list>))
(accum '() (method (x y) (cons (f x) y)) l)))
(define (intersection-set <function>)
(method ((s <list>) (t <list>))
(cond ((null? s) '())
((element-of-set? (head s) t)
(adjoin-set (head s) (intersection-set (tail s) t)))
(else: (intersection-set (tail s) t)))))
T(n) = T(n-1) + O(m) + C
= T(n-2) + 2 O(m) + 2C
...
= n O(m) + n C = O(mn).
T(n-1) is the recursive call,
O(m) is the call to element-of-set? and adjoin-set.
(define (intersection-set <function>)
(method ((s <list>) (t <list>))
(cond ((or (null? s) (null? t)) '())
((< (head s) (head t)) (intersection-set (tail s) t))
((> (head s) (head t)) (intersection-set s (tail t)))
(else: (adjoin-set (head s) (intersection-set (tail s) (tail t)))))))