这个问题是关于sbcl - 或者我原本以为。问题:什么时候角色不是角色?请看下面的代码:sbcl(和clisp):何时一个角色不是角色? (使用defconstant)
(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的回答,我本来期望它产生了一些关于符号,而不是一个字符,第二个表达式。
#\换行符是读取器的语法,它构造在读时间换行符对象。 –
有些东西可能会帮助你解决第二个问题:'(类型(第一'(1 2)))'应该是什么? – tfb