If the string in lisp is a vector, why can't I access the first element using svref?

So, I'm trying to learn Lisp, and I ran into a problem in determining what String is.

I read ANSI Common Lisp by Paul Graham, and this book says that String is a vector or a one-dimensional array.

So, I am creating a line:

(defvar *my-string* "abc") 

And then I can access the first value of my string as follows:

 (aref *my-string* 0) 

But if it's a vector, why can't I access this element this way:

 (svref *my-string* 0) 

I mean, when I create a vector this way:

 (defvar my-vec (make-array 4 :initial-element 1)) 

I can access the first item using svref:

 (svref my-vec 0) ; returns 1 

I forgot to add an error when I try svref on a String:

"The value" abc "is not of type (SIMPLE-ARRAY T (*))."

+6
source share
2 answers

String is a vector, but it is not a simple vector. svref takes a simple vector as the first argument.

You can verify this by calling:

 (vector-p *my-string*) 

which returns true

Unlike:

 (simple-vector-p *my-string*) 

which returns false.

Note that (simple-vector-p my-vec) will also return true, which confirms that make-array creates a simple vector.

+8
source

answercheck answer is absolutely right, but it's worth it with HyperSpec. For example, if you start on the svref page, the following appears at the bottom:

Notes:

svref is identical to isf , except that its first argument requires a simple vector .

The glossary dictionary for a simple vector (linked above) says:

simple vector . a vector of type simple vector , sometimes called a "simple common vector." Not all simple vectors β€” simple vectors β€” are only those that have the element type t .

15.2 The dictionary of arrays is also useful here, as well as 15. Arrays in general.

+5
source

Source: https://habr.com/ru/post/976158/


All Articles