Fundamentals of Computer Science I: Media Computing (CS151.01 2008S)

Vectors


Building Vectors

(make-vector length val)
Standard Vector Procedure. Create a new vector of length length, containing only copies of val.
(vector val_0 val_1 ... val_n)
Standard Vector Procedure. Create a new vector of size n+1 of the form #(val_0 val_1 ... val_n).

Accessing Elements by Index

(vector-ref vec n)
Standard Vector Procedure. Get the nth element of vec. Note that elements are numbered starting at 0.
(vector-set! vec k val)
Standard Vector Procedure. Set the kth element of vec to val. (Note that vectors use 0-based indexing.)

Converting Vectors

(list->vector lst)
Standard Vector Procedure. Convert lst to a vector so that the ith value in the vector is the same as the ith value in the lst.
(vector->list vec)
Standard Vector Procedure. Convert vec to a list so that the ith value in the list is the same as the ith value in the vector.

Miscellaneous

(vector? val)
Standard Vector Predicate. Determine if val is a vector.
(vector-fill! vec val)
Standard Vector Procedure. Fill vec with multiple copies of val.
(vector-length vec)
Standard Vector Procedure. Determine the size of vec.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit 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.