ANDing boolean and list

I noticed in Scheme, Racket, and Clojure that the expression (using Clojure here) (and true '()) evaluates to () , and (and '() true) true . This is true not only for an empty list, but for any list.

But in GNU CLISP and Emacs Lisp, (and t '()) is rated as nil and (and '() t) also rated as nil , but (and t '(1 2 3)) is rated as (1 2 3) and (and '(1 2 3) t) is estimated as t .

What's going on here?

+5
source share
3 answers

In the first group of languages, an empty list is not treated as "falsey" and instead is treated as a "true" value. In a pattern and racket, #false is the only false value, so even if '() is null, null is not false; in clojure, an empty list does not match nil, so it is also "true".

In the second group, an empty list is synonymous with nil and is treated as false, which causes nil to return. However, a list with items is not the same as zero, and therefore is again considered to be a true value.

The final piece of the puzzle is that and returns the last right value if all the values โ€‹โ€‹passed are true.

+9
source

In Clojure, only false and nil are considered logically false. Everything else is considered logically true.

In the other Lisps you are talking about, an empty list is considered logically false.

+2
source

The and operator evaluates the arguments and the "shortcircuits" result, i.e. as soon as one argument is false, it returns nil. Otherwise, the last value is returned. The difference in behavior is that in Common Lisp an empty list is the same as nil , which is false, so (and '() t) same as (and nil t) , which returns nil .

+2
source

Source: https://habr.com/ru/post/1259924/


All Articles