在 Common Lisp 中不带括号和引号的函数中使用符号参数
Using symbol parameters in a function without parentheses and quote in Common Lisp
(defun all-longer-than-1-char? (&rest elements)
(every (lambda (x) (> (length
(cond ( (typep x 'integer) (write-to-string x) )
( (typep x 'string) x )
( (typep x 'symbol) (symbol-name x) )
))
1))
elements))
(all-longer-than-1-char? "OK" "NO" 1)
我希望此函数适用于交易品种参数(即无需双引号或输入数字),但它不起作用。要使其与交易品种参数一起使用:
(defun all-longer-than-1-char? (lst)
(every (lambda (x) (> (length
(cond ( (typep x 'integer) (write-to-string x) )
( (typep x 'string) x )
( (typep x 'symbol) (symbol-name x) )
))
1))
lst))
(all-longer-than-1-char? '(OK NO 1))
NIL
但是这次我必须将参数括在括号内并用引号引起来。我想让它同时使用符号参数,而不必将参数放在括号内并引用它们,例如:
(all-longer-than-1-char? OK NO 1)
怎么做?
您可以使用 &rest
来创建曾经被称为 'nospread' 函数(或 'lexpr' 取决于您的宗教信仰)的函数,除了作为用户界面,因为如果你有一个列表,那么你必须使用 apply
.
Common Lisp 没有不计算参数的函数,这曾经是所谓的 'nlambda'(或者 'fexpr',如果你属于错误的邪教),所以你需要引用对评估者有意义的表格。
您可以获得与使用宏的 nlambda 相同的结果。但是您几乎肯定不想这样做,因为它闻起来像是对宏的错误使用。
给出
(defun all-longer-than-1-char-p (list)
(every (lambda (x)
(> (length
(etypecase x
(string x)
(integer (write-to-string x))
(symbol (symbol-name x))))
1))
list))
那么没有传播的可能是
(defun all-longer-than-1-char-p/nospread (&rest list)
(all-longer-than-1-char list))
nlambda 可能是
(defmacro all-longer-than-1-char-p/quoted/nospread (&rest things)
`(all-longer-than-1-char ',things))
所以现在
> (all-longer-than-1-char-p '(xx yy 12 "foo"))
t
> (all-longer-than-1-char-p/nospread 'xx 'yy 12 "foo")
t
> (all-longer-than-1-char-p/quoted/nospread xx yy 12 "foo")
t
(假设 *print-base*
小于 13
)。
但是
> (let ((x "xx"))
(all-longer-than-1-char-p/quoted/nospread x))
nil
所以,在语义上不是很有用,是如何不使用宏的典型代表。
(defun all-longer-than-1-char? (&rest elements)
(every (lambda (x) (> (length
(cond ( (typep x 'integer) (write-to-string x) )
( (typep x 'string) x )
( (typep x 'symbol) (symbol-name x) )
))
1))
elements))
(all-longer-than-1-char? "OK" "NO" 1)
我希望此函数适用于交易品种参数(即无需双引号或输入数字),但它不起作用。要使其与交易品种参数一起使用:
(defun all-longer-than-1-char? (lst)
(every (lambda (x) (> (length
(cond ( (typep x 'integer) (write-to-string x) )
( (typep x 'string) x )
( (typep x 'symbol) (symbol-name x) )
))
1))
lst))
(all-longer-than-1-char? '(OK NO 1))
NIL
但是这次我必须将参数括在括号内并用引号引起来。我想让它同时使用符号参数,而不必将参数放在括号内并引用它们,例如:
(all-longer-than-1-char? OK NO 1)
怎么做?
您可以使用 &rest
来创建曾经被称为 'nospread' 函数(或 'lexpr' 取决于您的宗教信仰)的函数,除了作为用户界面,因为如果你有一个列表,那么你必须使用 apply
.
Common Lisp 没有不计算参数的函数,这曾经是所谓的 'nlambda'(或者 'fexpr',如果你属于错误的邪教),所以你需要引用对评估者有意义的表格。
您可以获得与使用宏的 nlambda 相同的结果。但是您几乎肯定不想这样做,因为它闻起来像是对宏的错误使用。
给出
(defun all-longer-than-1-char-p (list)
(every (lambda (x)
(> (length
(etypecase x
(string x)
(integer (write-to-string x))
(symbol (symbol-name x))))
1))
list))
那么没有传播的可能是
(defun all-longer-than-1-char-p/nospread (&rest list)
(all-longer-than-1-char list))
nlambda 可能是
(defmacro all-longer-than-1-char-p/quoted/nospread (&rest things)
`(all-longer-than-1-char ',things))
所以现在
> (all-longer-than-1-char-p '(xx yy 12 "foo"))
t
> (all-longer-than-1-char-p/nospread 'xx 'yy 12 "foo")
t
> (all-longer-than-1-char-p/quoted/nospread xx yy 12 "foo")
t
(假设 *print-base*
小于 13
)。
但是
> (let ((x "xx"))
(all-longer-than-1-char-p/quoted/nospread x))
nil
所以,在语义上不是很有用,是如何不使用宏的典型代表。