MASSACHVSETTS INSTITVTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001---Structure and Interpretation of Computer Programs
Spring Semester, 1999
Recitation -- Wednesday, March 1
What are the values of the following expressions:
Write the following procedures:
What's the order of growth of the procedure copy-some above?
. Consider the following procedure to copy the
last n elements of a list. What is the order of growth of
last-n?
.
(define (last-k k lst) (if (= (length lst) k) lst (last-k k (cdr lst))))
Notice that it can take a long time to find the length of one of our list structures. Say we want to define a new sequence abstraction, similar to lists, but that can return the length in constant time. Here's the contract:
(head (attach x seq)) == x (tail (attach x seq)) == seq (seq-empty? empty-seq) == #t (seq-empty? (attach x seq)) == #f (seq-length seq) == the length intime.
Notice that with either of these abstractions, lists behave in the same way as they did before, except that the length of a list can be computed in constant time. We traded time for space.