Lisp 中的布尔返回函数

时间:2021-03-13 01:43:28

标签: lisp common-lisp

有没有办法在 Lisp 中只通过调用函数名来返回 t 或 nil?下面的代码是不言自明的,但它不起作用,我不知道为什么。我知道还有其他方法可以获得相同的结果,但我这样做是为了了解布尔返回函数在 Lisp 中的工作原理。

(print "Enter any number to check if it is greater than 5.")
(defvar v (read))
(if (check v) (print "It is greater than 5") (print "It is not greater than 5"))

(defun check(x)
    (if (> x 5) (t) (nil))
)

1 个答案:

答案 0 :(得分:4)

你不需要这样做

(if <condition> t nil)

因为 <condition> 在测试时已经返回 tnil。 这样做

(defun check (x) <condition>)

在这种情况下

(defun check (x) (> x 5))