Common lisp 以句点结尾的反引号符号列表
我很好奇,是否有一种方法可以在逗号插入值后加上句点来结束反引号符号列表 以下是示例代码:Common lisp 以句点结尾的反引号符号列表,common-lisp,symbols,Common Lisp,Symbols,我很好奇,是否有一种方法可以在逗号插入值后加上句点来结束反引号符号列表 以下是示例代码: (defparameter *things* '(book pencil shoe)) (defun inspect-item (item things) (if (member item things) `(you pick up the ,item and yeet it out the window.) `(only realize the truth... t
(defparameter *things* '(book pencil shoe))
(defun inspect-item (item things)
(if (member item things)
`(you pick up the ,item and yeet it out the window.)
`(only realize the truth... there is no ,item.)))
这将成功打印检查项“book*things”,并生成符号列表,您可以拿起书并将其从窗口中取出。。在这种情况下,我假设句点是符号窗口的一部分。如使用上一个函数确认的
但是,这将无法打印检查项“spoon*things*”以声明该变量项。没有值,因为它认为名称是item。。在item和句点之间留一个空格会导致点列表非法结束的错误,我认为这是因为它假设我使用的是点列表语法
有没有办法让它产生我想要在书尾的符号?解决方案的可能要求
您需要基于旧符号创建新符号:
同一个名字,带一个字母。附加
可能在同一个包裹里
作为符号的句号
请注意,您可以使用转义符号以句号作为名称编写符号:|.|或\
princ打印不带转义字符:
CL-USER 18 > (princ '(ONLY REALIZE THE TRUTH... THERE IS NO FOOBAR \.))
(ONLY REALIZE THE TRUTH... THERE IS NO FOOBAR .) ; <- printed output
(ONLY REALIZE THE TRUTH... THERE IS NO FOOBAR \.) ; <- REPL value
使用格式的灵活性而不是使用连接也很有用
我认为真正的问题在于试图将文本作为符号列表的印刷表示形式。这些是完全不同的事情。这样做的唯一原因可能是某种锻炼。有很多介绍性的文章都是这样做的 相反,我建议实际生成文本。E串。您可以使用非常灵活的format函数来插入变量:
(defparameter *things* '("book" "pencil" "shoe"))
(defun inspect-item (item things)
(if (member item things :test #'string-equal)
(format nil
"You pick up the ~a and yeet it out the window."
item)
(format nil
"Only realize the truth… there is no ~a."
item)))
我部分同意斯万特的观点,将符号列表转换成文本通常是错误的。另一方面,像解析器&c这样的东西通常希望用符号而不是字符串来思考,因为符号有很好的属性,比如eq a b,以您希望的方式工作 也就是说,这里有另一种方法:创建一个单词列表,然后使用一个函数将它们转换为表示句子的字符串,添加适当的标点符号和大写字母
(defparameter *things* '(book pencil shoe))
(defun inspect-item (item things)
(sentencify (if (member item things)
`(you pick up the ,item and yeet it out the window)
`(only realize the truth... there is no ,item))))
(defun sentencify (slist &key (question nil))
(format nil "~{~A~^ ~}~A"
(loop for first = t then nil
for w in slist
for ws = (typecase w
(symbol (symbol-name w))
(string w)
(t w))
collect (typecase ws
(string (if first
(string-capitalize ws)
(string-downcase ws)))
(t ws)))
(if question "?" ".")))
现在:
> (inspect-item 'book *things*)
"You pick up the book and yeet it out the window."
> (inspect-item 'bok *things*)
"Only realize the truth... there is no bok."
> (inspect-item '(x . y) *things*)
"Only realize the truth... there is no (x . y).
哦,当然可以。这并不是一种正确的做事方式,而是一种对极限的好奇探索。在任何正常情况下,都会使用字符串。
(defparameter *things* '("book" "pencil" "shoe"))
(defun inspect-item (item things)
(if (member item things :test #'string-equal)
(format nil
"You pick up the ~a and yeet it out the window."
item)
(format nil
"Only realize the truth… there is no ~a."
item)))
(defparameter *things* '(book pencil shoe))
(defun inspect-item (item things)
(sentencify (if (member item things)
`(you pick up the ,item and yeet it out the window)
`(only realize the truth... there is no ,item))))
(defun sentencify (slist &key (question nil))
(format nil "~{~A~^ ~}~A"
(loop for first = t then nil
for w in slist
for ws = (typecase w
(symbol (symbol-name w))
(string w)
(t w))
collect (typecase ws
(string (if first
(string-capitalize ws)
(string-downcase ws)))
(t ws)))
(if question "?" ".")))
> (inspect-item 'book *things*)
"You pick up the book and yeet it out the window."
> (inspect-item 'bok *things*)
"Only realize the truth... there is no bok."
> (inspect-item '(x . y) *things*)
"Only realize the truth... there is no (x . y).