How does "or" work in Clojure?

Why does the second operation evaluate to false? Is the argument "or" different in clojure? If so, how can I write an operation so that it evaluates to true no matter where the argument 0 is?

(= (or 0 1) 0) ; true

(= (or 1 0) 0) ; false
+4
source share
5 answers

ornot a function - this is a macro. it expands to challenges, ifand thus the rules apply if: nilor they falseare flexible, everything else is true - including 0.

user=> (when 0 (println "true"))
true
nil

Your code expands to:

user=> (macroexpand '(or 1 0))
(let* [or__4238__auto__ 1] (if or__4238__auto__ or__4238__auto__ (clojure.core/or 0)))

So, the orgenerated code returns the first true argument - or the last.

, , , - :

user=> (some zero? [1 2 3])
nil
user=> (some zero? [1 2 0])
true

, 0 :

user=> (some #{0} [1 2 3])
nil
user=> (some #{0} [1 2 0])
0
+3

(or)(or x)(or x & next) - , .

true, or - , nil, falsy (: Clojure documentation).

(= (or 0 1) 0) ( 0 1) 0, true ( clojure inly nil false ), 0 true.

(= (or 1 0) 0) 1 0 false, .

+5

false. , , .

+3

Clojure nil false , .

, , , , . (some zero? [0 1])

+3

. Clojure.

(or 0 1) ; 0
(or 1 0) ; 1

... as both 0and 1logically true, and orreturns its first argument is logically true (or the last argument if there is no true). The only thing that is not true is niland false(and its box form Boolean/FALSE).

The fact that it oris a macro is random in this case.

+2
source

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


All Articles