【发布时间】:2017-10-21 03:41:10
【问题描述】:
这个问题是关于 sbcl 的——至少我最初是这么想的。问题:什么时候一个角色不是一个角色?考虑以下代码:
(defconstant +asc-lf+ #\Newline)
(defconstant +asc-space+ #\Space)
(prin1 (type-of #\Newline )) (terpri)
(prin1 (type-of #\Space )) (terpri)
(prin1 (type-of +asc-lf+ )) (terpri)
(prin1 (type-of +asc-space+)) (terpri)
正如预期的那样,它会产生:
STANDARD-CHAR
STANDARD-CHAR
STANDARD-CHAR
STANDARD-CHAR
现在考虑这段代码:
(defun st (the-string)
(string-trim '(#\Newline #\Space) the-string))
(princ "\"")
(princ (st " abcdefgh "))
(princ "\"")
(terpri)
它产生:
"abcdefgh"
但考虑一下这段代码:
(defconstant +asc-lf+ #\Newline)
(defconstant +asc-space+ #\Space)
(defun st (the-string)
(string-trim '(+asc-lf+ +asc-space+) the-string))
(princ "\"")
(princ (st " abcdefgh "))
(princ "\"")
(terpri)
当您使用 sbcl 加载它时,它会为您提供:
While evaluating the form starting at line 6, column 0
of #P"/u/home/sbcl/experiments/type-conflict.d/2.lisp":"
debugger invoked on a TYPE-ERROR:
The value
+ASC-LF+
is not of type
CHARACTER
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [RETRY ] Retry EVAL of current toplevel form.
1: [CONTINUE] Ignore error and continue loading file "/u/home/sbcl/experiments/type-conflict.d/2.lisp".
2: [ABORT ] Abort loading file "/u/home/sbcl/experiments/type-conflict.d/2.lisp".
3: Exit debugger, returning to top level.
((FLET SB-IMPL::TRIM-CHAR-P :IN SB-IMPL::GENERIC-STRING-TRIM) #\ )
0]
起初,我期待能够报告 clisp 对 #'string-trim 进行了适当的调用,并带有预期的返回值,或者可能会出错。但它都不做这些。该函数返回传递给它的相同字符串,没有任何修剪。
这是应该发生的事情吗?我错过了什么?
编辑大约。 2017-10-21 08:50 UTC
PuercoPop 的精彩回答引发了后续问题。如果我应该将此作为一个单独的问题发布,只需说出这个词,我会的。
为什么会这样(至少对于 sbcl 和 clisp):
(defconstant +asc-lf+ #\Newline)
(defconstant +asc-space+ #\Space)
(prin1 (type-of (first (list #\Newline #\Space))))
(terpri)
(prin1 (type-of (first '(#\Newline #\Space))))
(terpri)
会这样吗?
STANDARD-CHAR
STANDARD-CHAR
有了 PuercoPop 的回答,我原以为它会为第二个表达式产生一些关于符号而不是字符的东西。
【问题讨论】:
-
#\Newline 是阅读器语法,它在阅读时构造一个换行符对象。
-
第二个问题可能对您有所帮助:
(type-of (first '(1 2)))应该是什么?
标签: lisp common-lisp sbcl clisp