CS2322
Laboratory Problem 9
More work with n-tuples
In this problem we wish to add functionality to our n-tuple processing functions. At present we can add, subtract, multiply and divide n-tuples. In addtion we can operate on n-tuples with single numbers. Finally, the count function from Laboratory Problem 2 provides a way of generating n-tuples of the counting indices. Also, the function length gives us the number of elements in an n-tuple while append allows us to catinate n-tuples.
One new function we desire is nth. nth has the following form:
(define nth
(lambda (n n-tuple)
...))
nth returns the n-1st element of an n-tuple. This is referred to as zero origin indexing. For example:
(nth 4 '(2 3 4 5 9 11 15)) ==> 9
(nth 0 '(2 3 4 5 9 11 15)) ==> 2
(nth 10 '(2 3 4 5 9 11 15)) ==> ()
Modify the function count so that it returns the indices origin 0, that is, so that
(count 5) ==> 0 1 2 3 4
Another pair of functions desired are take and drop. These functions have the form
(define take
(lambda (n n-tuple)
...))
(define drop
(lambda (n n-tuple)
...))
These functions return the first n elements or remove the first n elements of an n-tuple. For example:
(take 3 (count 5)) ==> (0 1 2)
(take 0 (count 5)) ==> ()
(take 10 (count 5)) ==> (0 1 2 3 4)
(drop 0 (count 5)) ==> (0 1 2 3 4)
(drop 3 (count 5)) ==> (3 4)
(drop 10 (count 5)) ==> ()
Lab 9 Scheme Code