In clojure, (= 'a' a) refers to the "single atom"?

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

(eq 'a 'a) 

It is true because 'a and 'a are "the same atom".

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

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

Now Clojure on the JVM inherits the Java legacy, but is it true to say that two atoms in Clojure (in the JVM) that have the same value are the same atom? (for example, how does the Clojure atom storage engine work?)

+6
java lisp clojure scheme
source share
4 answers

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

Function

Clojure = uses value-based equality. Thus, two objects with equal values ​​will be = , even if they are stored in different places in memory.

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

+21
source share

I think that "a" and "a" will be different Java objects under the hood. I believe this confirms the suspicion:

 user> (def foo 5) #'user/foo user> (System/identityHashCode 'foo) 578999228 user> (System/identityHashCode 'foo) 1724482638 

If you look at the actual implementation of Symbol in Clojure, you will see that the symbol consists of a namespace and a name and these strings MUST be interned strings. The Symbol.equals () method relies on performing identity checks on these two strings based on the intern string.

+6
source share

I will explain part of Common Lisp:

Common Lisp (eq 'a' a) always returns T.

Reason: while reading, the reader looks up, and for both he will look for the same character a. Since any character is an EQ for itself, the expression always returns T.

This is true for most types of objects, but with a few exceptions. Numbers and characters, for example, do not need EQ in Common Lisp. The reason for this is efficiency. To compare them if they have the same number or the same character, you can use the EQL function.

+4
source share

To add the answers of Alex and Stuart, Clojure characters cannot be identical? if they = mainly because they can carry metadata. Two characters that have the same components .name and .namespace but different metadata will be = , but not identical? .

Can we assume that everything can be organized so that two characters with the same metadata, namespace and name are always identical? , but these are (1) two big problems for real gain (since you still have Symbols = characters, but not identical? ), (2) contrary to the idea that types that can carry metadata should usually be compared for equal value (to which metadata does not contribute), while the actual equality of the pointer should be reserved for special situations (mainly including interop).

Note that Clojure Keywords are a separate type for which = really equivalent equivalent identical? . (Therefore, they cannot have attached metadata.)

+4
source share

All Articles