Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This lab is also available in PDF.
Summary: In this laboratory, you will explore various aspects of the Vector data type that Scheme provides as an alternative to lists.
Contents:
Tell DrScheme not to print the lengths of vectors by entering
(print-vector-length #f)
.
a. In DrScheme's interaction window, type in a vector literal that denotes a vector containing just the two elements 3.14159 and 2.71828. How does DrScheme display the value of this vector?
b. Create a vector that contains the same two values by using the
vector
procedure.
c. Create a vector that contains the same two values by using the
make-vector
and vector-set!
procedures.
Consider the following code.
> (define aardvark (list 1 2 3 4)) > (define baboon aardvark) > (define aardvark (cons 5 (cdr aardvark))) > (define chinchilla (vector 1 2 3 4)) > (define dingo chinchilla) > (vector-set! chinchilla 0 5)
a. What do you expect the output of the following commands to be?
> (list-ref aardvark 0) _____ > (list-ref baboon 0) _____
b. Verify your answer experimentally.
c. What do your results suggest about Scheme?
d. What do you expect the output of the following commands to be?
> (vector-ref chinchilla 0) _____ > (vector-ref dingo 0) _____
e. Verify your answer experimentally.
f. What do your results suggest about Scheme?
a. Tell DrScheme to print the length of vectors by entering (print-vector-length #t)
.
b. Enter each of the following vector expressions in DrScheme; consider the result (perhaps by examining individual elements with vector-ref
); and indicate what vector has been created.
#4(0)
#4(1)
#4(1 2)
#2(1 2 3 4)
(make-vector 4 0)
c. Tell DrScheme not to print the lengths of vectors. Re-enter each expression. Do your results differ? What do the differences suggest?
Write a procedure, (vector-sum numbers)
,
which takes one argument, a vector
of numbers, and returns the sum of the elements of that vector.
You can
use vector->list
from the reading as a pattern for
vector-sum
-- only a few judicious changes are needed.
However, you should not use vector->list
as a helper.
In the reading on vectors, we saw
that it was possible to implement list->vector
and
vector->list
by using more primitive operations
(particularly vector-set!
and vector-length
).
Write your own version of vector-fill!
. Remember that
vector-fill!
takes two parameters, a vector and a
value, and puts that value in every position of the vector.
Note: You may find that you want to do two things for a particular
position: fill the value at that position and recurse. Remember that
when you want to sequence actions, you should use a begin
clause: (begin exp1 exp2)
.
Write a procedure, (rotate! vec)
that rotates
the elements in vec. That is, rotate!
puts
the initial element of vec at the end, the element at position
1 in position 0, the element at position 2 in position 1, and so on
and so forth.
a. Write a procedure, (reverse-vector vec)
,
that creates a new vector whose elements appear in the reverse order
of the elements in vec.
b. Write a procedure, (reverse-vector! vec)
,
that reverses vec in place
. That is, instead of
producing a new vector, it rearranges the elements within vec.
Write a procedure, (rotate! vec amt)
, that
rotates the values in vec by amt positions. That is,
the first amt values in vec move to the end, the value
in position amt moves to position 0, the value in position
amt+1 moves to position 1, and so on and so forth.
Just as in the case of list->vector
, you will probably
want to define a helper procedure that fills only part of the vector.
Your termination condition will certainly be different and should probably
involve the length of the vector.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Labs/vectors.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:32 2007.
The source to the document was last modified on Thu Mar 15 22:07:11 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/vectors.html
.
http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.