Why empty list is considered atom in Scheme?

134 views Asked by At

As far I know, atoms are any of - numbers, booleans and strings in Scheme. But when I run the atom? function on empty list - (atom? '()) - it returns true #t value.

What am I missing here? Does it have to with my Scheme language implementation i.e. Chicken Scheme?

3

There are 3 answers

0
Sylwester On BEST ANSWER

atom comes from McCarthy's original 1960 lisp paper. It was a lisp with two types, symbols and pairs. The empty list is just a synonym for the symbol nil and it was self evaluating. (atom ()) is the same as (atom nil) and both are true while (atom (cons x y)) evaluates to the false value nil. Common Lisp is a direct descendant with more than two types, however it still works the same for code that only cares about pairs and symbols. As a design decsetion anything that is not pairs are atoms and thus:

(defun (atom x)
  (not (consp x)))

Scheme standards, as in the RNRS and R7RS being the latest, does not have atom? as a primitive. When that said it might be that some scheme implementation has it. While it would be least surprising to be implemented in the same way as Common Lisp, namely:

(define (atom? x)
  (not (pair? x)))

Paul Graham wrote an excellent article called The roots of Lisp which I highly recommend.

0
molbdnilo On

There is no mention of any atom concept in any Scheme standard that I can find.

In Common Lisp it is anything that's not a pair, and Scheme implementations that provide atom? most likely stick to that.

2
ignis volens On

I am not sure if any Scheme standards define an atom? predicate. However the traditional Lisp definition would be that an atom is any object which is not a cons.

Since a list is defined as being either the special empty list object which is not a cons, or a cons of any object and a list, it follows that the empty list object is an atom.