[英]How to I check whether a given variable value is of type string
Essentially I'd say that you'll have to use (typep var 'string-type)
, but there is no such type as string as far as I known. 基本上我会说你必须使用
(typep var 'string-type)
,但据我所知,没有像字符串这样的类型。
Determining a type via type-of results in 通过类型结果确定类型
(type-of "rowrowrowyourboat")
> (SIMPLE-ARRAY CHARACTER (17))
which is hardy a type I could look for in a generic way as looking for just SIMPLE-ARRAY
wouldn't do any good: 这是一种我能用通用方式寻找的类型,因为寻找
SIMPLE-ARRAY
不会有任何好处:
(typep "rowrowrowyourboat" 'simple-array)
> t
(typep (make-array 1) 'simple-array)
> t
And using a intuitive the hack of dynamically determining the type of an example string doesn't do any good either as they will not be of the same length (most of the time) 并且使用直观的动态确定示例字符串类型的hack也没有任何好处,因为它们的长度不同(大多数时候)
(typep "rowrowrowyourboat" (type-of "string"))
> nil
So I wonder what is the canonical way to check whether a given variable is of type string? 所以我想知道检查给定变量是否为string类型的规范方法是什么?
Most types has a predicate in CL and even if a string is a sequence of chars it exists a function, stringp , that does exactly what you want. 大多数类型在CL中都有一个谓词,即使字符串是一系列字符,它也存在一个函数stringp ,它完全符合你的要求。
(stringp "getlydownthestream") ; ==> T
It says in the documentation that that would be the same as writing 它在文档中说,这与写作相同
(typep "ifyouseeacrocodile" 'string) ; ==> T
Your question has several misconceptions. 你的问题有几个误解。 Usually in Lisp variables don't have a type.
通常在Lisp变量中没有类型。
Repeat: Variables in Lisp have no types . 重复: Lisp中的变量没有类型 。
You can ask if some Lisp value is of some type or what type it has. 您可以询问某些Lisp值是某种类型还是具有哪种类型。 Lisp objects have types attached.
Lisp对象附加了类型。
Common Lisp has no type 'string'? Common Lisp没有类型'string'? Why don't you look into the documentation?
你为什么不查看文档? It is easy.
这很容易。
Common Lisp HyperSpec: http://www.lispworks.com/documentation/HyperSpec/Front/index.htm Common Lisp HyperSpec: http : //www.lispworks.com/documentation/HyperSpec/Front/index.htm
Symbol Index: http://www.lispworks.com/documentation/HyperSpec/Front/X_Symbol.htm 符号索引: http : //www.lispworks.com/documentation/HyperSpec/Front/X_Symbol.htm
Symbol Index for S: http://www.lispworks.com/documentation/HyperSpec/Front/X_Alph_S.htm S的符号索引: http : //www.lispworks.com/documentation/HyperSpec/Front/X_Alph_S.htm
STRING: http://www.lispworks.com/documentation/HyperSpec/Body/a_string.htm#string STRING: http : //www.lispworks.com/documentation/HyperSpec/Body/a_string.htm#string
System Class STRING: http://www.lispworks.com/documentation/HyperSpec/Body/t_string.htm 系统类STRING: http : //www.lispworks.com/documentation/HyperSpec/Body/t_string.htm
So Common Lisp has a type STRING
. 所以Common Lisp有一个
STRING
类型。
The Strings Dictionary also lists other string types: BASE-STRING
, SIMPLE-STRING
, SIMPLE-BASE-STRING
. 字符串字典还列出了其他字符串类型:
BASE-STRING
, SIMPLE-STRING
, SIMPLE-BASE-STRING
。
I'm using LispWorks, so the returned types look a bit different: 我正在使用LispWorks,因此返回的类型看起来有点不同:
CL-USER 20 > (type-of "foo")
SIMPLE-BASE-STRING
CL-USER 21 > (typep "foo" 'string)
T
CL-USER 22 > (stringp "foo")
T
CL-USER 23 > (subtypep 'simple-base-string 'string)
T
T
CL-USER 24 > (let ((var "foo")) (typep var 'string))
T
Secret: variables in Common Lisp can be typed, but that is another story. 秘密:Common Lisp中的变量可以输入,但这是另一个故事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.