s c h e m a t i c s : c o o k b o o k

/ Cookbook.VectorIntroduction

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Introduction

Vectors are efficient data structures, containing a fixed number of elements that can be accessed in constant time, and in any order. Vectors are mutable, though 'growing' a vector can be costly (as opposed to lists, which can grow at very little cost).

Vectors are printed in Scheme as a hash mark (#) followed by individual elements contained with a set of parenthesis.

(define test (vector 1 #\a "dog" 0.123 (/ 2 3)))

Like lists, vectors can contain any mixture of types. This ability allows vectors to be used to emulate object-oriented class-like mechanisms by storing functions as elements of the vector:

> (define a (vector 1 'a "dog" 0.123 (lambda (x) (+ 1 2))))
> a
#(1 'a "dog" 0.123 (lambda (x) (+ 1 2)))
> (vector-ref a 2)
"dog"
> ((eval (vector-ref a 4)) 1)
3 

The recipes in this section show various techniques to make the most of the vector data type.

-- BrentAFulgham - 21 Jan 2007

Notes

Although #(1 2 3) evaluates to a vector in PLT Scheme, it is often better to use (vector 1 2 3). The syntax #(1 2 3) indicates a literal constant, so it is an error to mutate the vector. With (vector 1 2 3) the call to vector allocates a new vector, which it is ok to mutate. Note also that in R5RS you need to quote vector constants, that is you need to write '#(1 2 3). Finally normal vectors aren't "growable", but see for example the package "evector.plt" at PLaneT, if you need a growable vector like data structure.

-- JensAxelSoegaard - 21 Jan 2007

CookbookForm
TopicType: Section
ParentTopic: VectorChapter
TopicOrder: 010

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest