lisp - 始终评估为 t(或任何值)的函数

标签 lisp common-lisp predicate

比如说,我们想使用一个需要谓词的函数,但出于某种原因我们对该函数的其他特性感兴趣(比如 :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/

相关文章:

list - 如何将空列表作为列表的元素返回

common-lisp - SBCL 意外陷入浮点?

lisp - 将 Lisp 语法与 (operator-integer-operator) 格式混淆

lisp - 如何通过知道其在 lisp 中的位置来查找列表元素?

java - 从 QueryDSL 谓词对象中获取参数

lisp - Lisp 中的项目可视化

string - Lisp - 将输入拆分为单独的字符串

java - 是否有一种方便的方法来创建一个测试字段是否等于给定值的谓词?

swift - 在 Cloudkit 谓词上使用 OR 运算符

lisp - 什么时候使用 initform?