HOME Yet Another Scheme Tutorial Download Post Messages

14. Vectors and Structures

1. Introduction

In this chapter, I am going to explain about vectors and structures.

Vectors are set of data indexed by integer. Different types of data can be stored in a same vector, which is different from arrays of the C language. Vectors are more compact than lists and the access time is shorter. On the other hand, as vectors are manipulated using side effects, it may cause bags.

Structures in the Scheme is similar to those in the C language. However, the structures in the Scheme is more convenient than those of the C language, because the Scheme writes accessors and setters for the structures automatically, which is one of the benefits of macro of the Lisp/Scheme programming languages.

2. Vectors

2.1. Literals

Vectors are represented by '#(' and ')' like #(1 2 3). They should be quoted when used as literals


'#(1 2 3)             ; a vector of integers
'#(a 0 #\a)           ; a vector consisting of a symbol, an integer, and a character

2.2. Functions for vectors

Following functions are defined in the R5RS.
(vector? obj)
It returns #t if obj is a vector.
(make-vector k)
(make-vector k fill)
It returns a vector with k elements. If the second argument (fill) is specified, each elements are initialized with fill.
(vector obj ...)
It returns a vector consisting of the arguments.
(vector-length vector)
It returns the length of the vector.
(vector-ref vector k)
It returns the k-th element of the vector.
(vector-set! vector k obj)
It sets the k-th element of vector to obj.
(vector->list vector)
It converts vector to a list.
(list->vector list)
It converts list to a vector.
(vector-fill! vector fill)
It sets all the items of vector to fill.
Example: A function that calculate the sum of vectors.
01:     (define (vector-add v1 v2)
02:       (let ((lenv1 (vector-length v1))
03:     	(lenv2 (vector-length v2)))
04:         (if (= lenv1 lenv2)
05:     	(let ((v (make-vector lenv1)))
06:     	  (let loop ((i 0))
07:     	    (if (= i lenv1)
08:     		v
09:     		(begin
10:     		  (vector-set! v i (+ (vector-ref v1 i) (vector-ref v2 i)))
11:     		  (loop (1+ i))))))
12:     	(error "different dimensions."))))

Exercise 1

Write a function that calculates the inner product of two vectors.

3. Structures

3.1. General Feature

While structures are not defined in the R5RS,
Structures similar to those of the Common Lisp is implemented in many Scheme implementations.

The reality of the structures are vectors. Each slot is named by using a macro, which I will explain in the next chapter (chapter 15). Structures express data with different kinds of attributes clearly. The macro that define the structure makes accessors and setters functions automatically. It is one of the greatest benefits of the Lisp/Scheme that you can write programs that write programs. By using this feature, you can write beautiful programs quickly.

3.2. Structures in the MIT-Scheme

In the MIT-Scheme, structures are defined by define-structure. I will explain using an example, because it is easier to understand.
Let's think about books. Books have following attributes: The structure book can be defined like as follows:
(define-structure book title authors publisher year isbn)
Following shows how to register "The Cathedral and Bazaar".
(define bazaar 
   "The Cathedral and the Bazaar"
   "Eric S. Raymond"
However, this way is inconvenient somehow because the association of attributes with values is not clear. The keyword-constructor option is available to solve this problem. Following code is the revised version using this option, in which the relation between attributes and values is clear. In addition, the order of attributes does not matter in this option. The copier option is available, which creates a copier function for the structure.
(define-structure (book keyword-constructor copier) 
  title authors publisher year isbn)

(define bazaar 
   'title "The Cathedral and the Bazaar"
   'authors "Eric S. Raymond"
   'publisher "O'Reilly"
   'year 1999
   'isbn 0596001088))