比如说,我们想使用一个需要谓词的函数,但出于某种原因我们对该函数的其他特性感兴趣(比如 :start
和 :end
参数),所以我们需要提供一个始终返回 true 的谓词。
显然,这根本不是问题:
CL-USER> (defparameter *list* '(0 1 2 3 4 5))
*LIST*
CL-USER> (remove-if (lambda (x) t) *list* :start 1 :end 3)
(0 3 4 5)
有效,但一点也不漂亮。我们可能会收到变量 x
未被使用的丑陋消息。由于我在 LISP 中是为了美观,所以我很好奇是否存在“总是 t”谓词?
我们可以定义它:
(defun tp (&rest rest)
(declare (ignore rest))
t)
..但它可能存在吗?
最佳答案
您正在寻找函数 constantly
它接受一个参数并返回一个始终返回该值的函数。那么你需要的谓词是(constantly t)
。因此:
CL-USER> (remove-if (constantly t) '(0 1 2 3 4 5) :start 1 :end 3)
(0 3 4 5)
关于constantly
的说明表明您在建议的实现方面绝对走在正确的轨道上。 (不过,通过添加 (declare (ignore …))
,你做得更好。)
Notes:
constantly could be defined by:
(defun constantly (object) #'(lambda (&rest arguments) object))
写完这篇文章后,我想到这可能是重复的。我没有找到合适的副本,找到了一个类似的、更具体的问题,该问题正在寻求删除某个位置的单个元素,Is there a common lisp macro for popping the nth element from a list? , 其中Rainer Joswig's answer包括:
Removing the nth element of a list:
(defun remove-nth (list n) (remove-if (constantly t) list :start n :end (1+ n)))
这实际上只是该方法的概括,因为您使用的是任意序列边界。因此我们可以(使边界参数类似于 subseq
的):
(defun remove-subseq (sequence &optional (start 0) end)
(remove-if (constantly t) sequence :start start :end end))
(defun remove-nth (sequence n)
(remove-subseq sequence n (1+ n)))
CL-USER> (remove-subseq '(0 1 2 3 4 5) 1 3)
(0 3 4 5)
CL-USER> (remove-nth '(0 1 2 3 4 5) 3)
(0 1 2 4 5)
关于lisp - 始终评估为 t(或任何值)的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24037628/