No-argument (and) returns t

我怕爱的太早我们不能终老 提交于 2019-12-08 19:16:09

问题


Both CL and Scheme define (and) to return t (or #t) with no arguments.

I'm trying to understand the rationale for this. My naive assumption is that an empty set is false, and passing in zero arguments feels like passing in nothing that can be true.

Edit: clojure follows the same convention. I must be missing some basic Lisp assumption.


回答1:


The empty product is 1. The reason is that 1 is a neutral element for *.

If you have the product of 2 and 3 and then multiply by the product of nothing, you will get 2*3*1 = 6. We can write

  (product (product 2 3) (product)) 
= (product 6 1) 
= 6

The same computation with and:

  (and (and #t #t) (and)) 
= (and #t ?) 
= #t

We want the empty (and) to give a value ? that doesn't affect the result. The answer is #t since #t is a neutral element.

(and x #t) = x   for all boolean x



回答2:


Here's a more intuitive answer: an "and" is like a checklist: you're "done" (that is, true) when all of the things on the list are true. Suppose someone gave you an empty checklist; in that case, you have nothing to check off, and you're trivially done.




回答3:


Here's my attempt to put it as simple as possible: An and expression is false if and only at least one of its arguments is false, otherwise it is true. So it is trivially true if there are no arguments.



来源:https://stackoverflow.com/questions/30467726/no-argument-and-returns-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!