In Clojure, (= [:a :b] (list :a :b))
return true
, but (= [:a :b] (:a :b))
return false
. Why?
I think that (list :a :b)
is (:a :b)
, so all should not return true
.
(f x)
asks to call function f
with argument x
. So, (:a :b)
calls :a
as a function, with :b
as its argument. What that actually does is not terribly important at the moment, but it certainly doesn't return the list (:a :b)
in the way that (list :a :b)
does. If you want to treat a list as a data structure rather than as a function call, you can quote
it, via (quote (:a :b))
. To get more details on what quoting is all about, you can read When to use 'quote in Lisp - it's not Clojure-specific, but still relevant.
From The Joy Of Clojure 2nd Edition:
The identical? function in Clojure only ever returns true when the symbols are in fact the same object:
(let [x (with-meta 'goat {:ornery true})
y (with-meta 'goat {:ornery false})]
[(= x y)
(identical? x y)
(meta x)
(meta y)])
;=> [true false {:ornery true} {:ornery false}]
Equals (=) compares contents of objects (without comparing meta-data, as shown above), identical? only true if they are the same object:
(let [x 'goat, y x]
(identical? x y))
;=> true