Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clojure, is (= 'a 'a) referring to the 'same atom'?

In some implementations of Common LISP we can say that for the following expression

(eq 'a 'a)

Is true because 'a and 'a are the "same atom".

This may be implementation dependent, but it seems the phrase (used in a popular LISP teaching book) assumes that atoms of the same value are stored in the same location in memory.

In Java, two interned strings of the same value are stored in the same location in memory.

Now Clojure on the JVM inherits Java's legacy, but is it true to say that two atoms in Clojure (on JVM) having the same value are the same atom? (ie how does Clojure's atom storage mechanism work?)

like image 677
hawkeye Avatar asked Sep 07 '10 08:09

hawkeye


1 Answers

First, "atom" has a different meaning in Clojure than in most other Lisps. See http://clojure.org/atoms

Clojure's = function uses value-based equality. So two objects with equal values will be = even if they are stored in different locations in memory.

To test if two objects are actually the same object, at the same address in memory, use the identical? function.

like image 54
Stuart Sierra Avatar answered Sep 27 '22 19:09

Stuart Sierra