Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?)

like image 510
hawkeye Avatar asked Sep 08 '10 06:09

hawkeye


People also ask

What is an atom in Clojure?

Atoms are a data type in Clojure that provide a way to manage shared, synchronous, independent state. An atom is just like any reference type in any other programming language. The primary use of an atom is to hold Clojure's immutable datastructures. The value held by an atom is changed with the swap!

What does atom mean in Lisp?

In Lisp, what we have been calling words are called atoms. This term comes from the historical meaning of the word atom, which means “indivisible”.

Is Clojure similar to Lisp?

Clojure is a member of the Lisp family of languages. Many of the features of Lisp have made it into other languages, but Lisp's approach to code-as-data and its macro system still set it apart. Clojure extends the code-as-data system beyond parenthesized lists (s-expressions) to vectors and maps.

Why is Clojure not a Lisp?

Clojure is a Lisp-1 and is not intended to be code-compatible with other dialects of Lisp, since it uses its own set of data structures incompatible with other Lisps. As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system.


2 Answers

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.

like image 181
Rainer Joswig Avatar answered Sep 16 '22 15:09

Rainer Joswig


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).

like image 45
Arthur Ulfeldt Avatar answered Sep 20 '22 15:09

Arthur Ulfeldt