Create and name a vector containing five copies of the number 7. Then
perform a series of mutations, using vector-set!, to replace each of
the elements of the vector with its position number -- that is, overwrite
the leftmost 7 in the vector with the number 0, the next one with the
number 1, and so on. After replacing all of the elements of the vector,
examine it again to make sure that the desired side effects actually took
place.
Develop a Scheme procedure zero-out-negatives! that takes any vector
of real numbers as its argument and destructively replaces each of the
negative values in the vector with 0, leaving the non-negative ones
unchanged. You should expect that this procedure will normally be invoked
only for its side effect, so it makes no difference what value the
procedure returns.
Develop a Scheme procedure reverse-vector! that takes any vector as
argument and reverses the order of its elements, in place.
> (define sample-1 (vector 'alpha 'beta 'gamma 'delta))
> (reverse-vector! sample-1)
> sample-1
#(delta gamma beta alpha)
> (define sample-2 (vector "A" "man," "a" "plan," "a" "canal--" "Panama!"))
> (reverse-vector! sample-2)
> sample-2
#("Panama!" "canal--" "a" "plan," "a" "man," "A")
> (define sample-3 (vector))
> (reverse-vector! sample-3)
> sample-3
#()
The spectrum of a list of natural numbers is a vector indicating the number of occurrences of each natural number up to and including the largest one that occurs in the list. The tally of the number of occurrences of a natural number k is found at position k of the vector.
For example, in the list (0 3 0 1 0 1 0 0 3 0 1), the natural number
0 occurs six times, the natural number 1 occurs three times, and the
natural number 3 occurs twice, so the spectrum of this list is the vector
#(6 3 0 2). The 0 entry in the spectrum indicates that the natural
number 2 did not occur at all.
Develop a Scheme procedure spectrum that constructs and returns the
spectrum of a given list of natural numbers.
> (spectrum '(0 3 0 1 0 1 0 0 3 0 1)) #(6 3 0 2) > (spectrum '(3 1 4 1 5 9)) #(0 2 0 1 1 1 0 0 0 1) > (spectrum '(12 12 12 12 24 12)) #(0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 1) > (spectrum '()) #()