These exercises are due at the beginning of class on Monday, April 21. In
each case, you should submit the source code for your procedure and also
show what happens when your procedure is invoked, in enough different cases
to satisfy me that it works as described. Once again, use submit to create a log file and
elm to mail it to me.
The norm of a vector is the square root of the sum of the squares
of its elements. Design, write, and test a procedure named
norm that takes any vector of real numbers as its argument and
returns the norm of that vector.
(norm '#(9 12 8)) ===> 17 (norm '#(1 2 3 4)) ===> 5.477225575051661 (norm '#()) ===> 0
Design, write, and test a procedure named cumulative-totals
that takes one argument, a vector vec of real numbers, and
returns another vector of the same length; each position of the vector that
is returned should contain the sum of the elements of vec up
to and including the corresponding position.
(print-vector-length #f) (cumulative-totals '#(3 1 4 1 5 9 2 6 5)) ===> #(3 4 8 9 14 23 25 31 36) (cumulative-totals '#(1 1/2 1/3 1/4 1/5 1/6 1/7)) ===> #(1 3/2 11/6 25/12 137/60 49/20 363/140) (cumulative-totals '#(-12 12 -12 12 12 -12 0 777)) ===> #(-12 0 -12 0 12 0 0 777) (cumulative-totals '#()) ===> #()
Design, write, and test a Scheme procedure named partition
that takes two arguments, a vector vec of strings and a string
pivot, and rearranges the elements of vec so that
every string that lexicographically precedes or equals the pivot is in a
lower-numbered position than any of the strings that lexicographically
follows the pivot. (A string s1 is said to
lexicographically precede another string s2 just in
case (string<? s1 s2) is #t.) In addition to
mutating vec, partition should return the number
of strings in vec that lexicographically precede or equal the
pivot. (Hint: It should be easy to determine this quantity during the
rearrangement process instead of computing it separately at the end.)
(define sample-1 '#("red" "white" "and" "blue"))
(partition sample-1 "grey") ===> 2
sample-1 ===> #("and" "blue" "red" "white")
;; In this example, it's all right if "and" and "blue" are reversed, or if
;; "red" and "white" are reversed, or both, after the partition; the only
;; requirement is that both "and" and "blue" must precede both "red" and
;; "white".
(define sample-2 '#("to" "be" "or" "not" "to" "be" "that" "is" "the"
"question"))
(partition sample-2 "can") ===> 2
sample-2
===> #("be" "be" "or" "not" "to" "to" "that" "is" "the" "question")
;; In this example, the two occurrences of "be" must be at the beginning of
;; the vector after the partition, but the other strings may be in any
;; order.
(define sample-3 '#("four" "score" "and" "seven" "years" "ago"))
(partition sample-3 "zymurgy") ===> 6
sample-3 ===> #("four" "score" "and" "seven" "years" "ago")
;; After the partition, the elements of this vector may be arranged in any
;; order.
(define sample-4 '#())
(partition sample-4 "anything") ===> 0
sample-4 ===> #()
This document is available on the World Wide Web as
http://www.math.grin.edu/courses/Scheme/exercise-4.html