【发布时间】:2016-06-19 17:43:15
【问题描述】:
当我有一个函数定义make-cd 并执行该函数以获得错误答案时。
(defun make-cd (title artist rating ripped)
'(:title title :artist artist :rating rating :ripped ripped))
(add-record (make-cd "Roses" "Kathy Mattea" 7 t))
(:TITLE TITLE :ARTIST ARTIST :RATING RATING :RIPPED RIPPED)
我应该使用(list ...) 来获得正确答案。
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped))
(add-record (make-cd "Roses" "Kathy Mattea" 7 t))
(:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)
这是为什么?
【问题讨论】:
-
'(X Y Z)是(QUOTE (X Y Z))的简写。QUOTE只是将给定对象作为文字数据返回,而不是对其进行评估。也就是说,在您的示例中,TITLE、ARTIST、RATING和RIPPED不是对具有这些名称的变量的引用;它们只是文字符号。 -
这就像不插入
'this is $WYSIWYG' // => 'this is $WYSIWYG'的常量字符串和插入"This is not $WYSIWYG" // => "This is not replaced"的字符串之间的区别。
标签: list lisp common-lisp evaluation quote