0

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?

Harshal Patil
  • 17,838
  • 14
  • 60
  • 126
  • Interestingly, the [definition of `atom?` used by the classic book The Little Schemer](https://stackoverflow.com/a/9531513/9952196) is that an empty list is *not* an atom. – Shawn May 12 '23 at 20:25
  • While [chicken's `atom?`](https://wiki.call-cc.org/man/5/Module%20\(chicken%20base\)#atom) "Returns `#t` if X is not a pair." which is in line with [Common Lisp's `atom`](http://www.lispworks.com/documentation/HyperSpec/Body/f_atom.htm) – Shawn May 12 '23 at 20:30

3 Answers3

2

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.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
1

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

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.

ignis volens
  • 7,040
  • 2
  • 12
  • That rule also makes vectors and records atoms, which is much odder imo. But you get used to it. – Shawn May 12 '23 at 19:41
  • 1
    @Shawn It's really just an archaeological curiosity I think: once the only non-atomic objects were conses (more-or-less: Lisp 1.5 has arrays but they are very second-class) so `atom` (as it was in those Lisps and still is in CL) was a good name. But then people start using it to mean 'not a cons' and now you can't change it. It's good that Scheme left it out. – ignis volens May 13 '23 at 07:07