If the string in LISP is a vector, why can’t I use svref to access the first element?

So, I'm trying to learn lisp. I have a problem defining what a string is

I'm reading Paul Graham's ANSI Common Lisp. In this book, it points out that string is a vector or one-dimensional array

So I create a string:

(defvar *my-string* "abc")

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 the element like this:

(svref *my-string* 0)

I mean, when I create vectors this way:

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

I can access the first element using svref:

(svref my-vec 0) ; returns 1

I forgot to add an error when trying to use svref on string:

"The value" ABC "is not of type (simple-array t (*))."

Solution

A string is a vector, but it is not a simple vector Svref takes a simple vector as the first parameter

You can call:

(vector-p *my-string*)

Return true

differ:

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

Return false

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>