Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "or" work in Clojure?

Tags:

clojure

Why does the second operation computes to false? Does the "or" argument function differently in clojure? If so, how should I write the operation so it computes to true regardless of where the 0 is in the argument?

(= (or 0 1) 0) ; true

(= (or 1 0) 0) ; false
like image 780
Zalán Józsa Avatar asked Dec 05 '22 01:12

Zalán Józsa


1 Answers

(or)(or x)(or x & next) - evaluates expressions one at a time, from left to right.

If form returns logical true value, or returns that value and doesn't evaluate any of the other expressions, otherwise it returns the value of the last expression or nil if last expression are falsy (Source: Clojure documentation ).

In first statement (= (or 0 1) 0) the (or 0 1) return 0 because it's is logical true( in clojure inly nil and false are falsy) then compare with 0 so result is true.

In second statement (= (or 1 0) 0) it returns 1 and compare it to 0 and returns false because they aren't equal.

like image 90
Andriy Ivaneyko Avatar answered Dec 29 '22 19:12

Andriy Ivaneyko