What is the difference between an atom in Common Lisp and an atom in Clojure?
Asked Answered
G

3

15

The following page talks about how atoms work in Clojure. It doesn't say a whole lot about the differences between atoms in Clojure and other lisp dialects.

What is the primary difference between an atom in Common Lisp and an atom in Clojure? (What is missing from the definition of atom in Clojure that exists in CL?)

Ghats answered 8/9, 2010 at 6:26 Comment(0)
E
22

Atoms in Clojure and atoms in Common Lisp (and most other Lisps) are two completely unrelated concepts. They have nothing to do with each other, other than having the same name.

There is no 'difference'. It would be asking what is the difference between a window in a house and a window on your computer screen? It does not make sense to identify differences, since the two concepts are not related.

'Atoms' in Clojure manage state.

'Atoms' in Lisp is a word for all data types that are not cons cells (like numbers, characters, strings, symbols, ...).

In Lisp the function ATOM is simply defined as:

(defun atom (object)
   (not (consp object)))

Since Clojure does not have cons cells and no function consp, it is not possible to say (not (consp object)). Thus there does not exist a Lisp concept like 'atom' in Clojure. Note that Clojure has a function cons, but it does not create cons cells like in Lisp.

Earnestineearnings answered 8/9, 2010 at 6:33 Comment(2)
It's worth noting that Clojure still has Lisp atoms in symbols, keywords, numbers, strings, etc., but it's just not part of the Clojure vocabulary.Inwards
@John Cromartie: that can't really be. Clojure has no cons cells. In Lisp atom is defined as 'not cons'. Since clojure has no cons cells, the concept of a Lisp atom is undefined in Clojure. Note that Clojure has a cons operation, but it does not create cons cells.Earnestineearnings
H
1

They are largely different and have a common conceptual basis for using the name 'Atom'

  • Atom in Common lisp refers to the idea of an indivisable thing like the origional meaning of an atom of matter.

  • Atom in clojure refers to a specific mutable data structure that changes 'atomically' that is a write to it either completes or it does not (and is subsequently retried)

the common idea is the indivisible concept. in CL its what the thing is and in Clojure its how the thing changes.

In Clojure Atoms are used when you need blocking mutable data that is not coordinated. for instance a single userId counter or something. Clojure also has coordinated mutable access in Refs (think bank account transfers) and atomic uncoordinated non-blocking mutable things in Agents (think log collectors for example).

Harlow answered 8/9, 2010 at 22:46 Comment(1)
Common Lisp has no idea of 'indivisible' things. A cons is just like a two-slot record - like a special purpose vector of two entries. The concept of an ATOM is just defined to mean that it is anything other than a cons cell. But the cons cell as a data structure is nothing special (as opposed to arrays, vectors, strings, structures, CLOS objects, ...), it is only special in Lisp because it used widely.Earnestineearnings
M
0

In all Lisps atoms are symbolic expressions that are not lists (except empty lists). They are also called atomic S-expressions. What's atomic (indivisible) in it? Historically the lists were something that could be divided into smaller parts where atoms (but not non-empty lists) contained within them couldn't.

In Clojure there are also atomic S-expressions but there is also a data structure called Atom which allows one to create mutable data objects that can be accessed by multiple threads. What's atomic in them? The operation.

If you modify an Atom its state will be successfuly changed or not. There won't be a situation that a half of it (e.g. some vector) will change and a half will not. If the operation is unsuccessful then it is retried and a current thread waits until it completes.

What is the difference between Lisp's atoms and Clojure's Atoms? The abstraction level. Lisp's atoms are a class of symbolic expressions, whereas Clojure's Atoms are a class of data structures used to handle shared data.

Mead answered 27/10, 2014 at 12:3 Comment(2)
That's plain wrong. A symbol is an atom, but it does not evaluate to itself. Actually in older Lisps a lot of other things did not evaluate to themselves. Evaluation for them was an error. Being an atom and evaluation is unrelated. atom mostly means this: everything which is not a non-empty list. Everything which is not a cons cell is an atom.Earnestineearnings
Thanks for correcting me. I was fixated on keywords when thinking about atoms. Removed wrong statement about self-evaluation.Mead

© 2022 - 2024 — McMap. All rights reserved.