4

In Clojure, I find this surprising:

> (count nil)
0

I would expect a type error, as in this case:

> (count 77)
java.lang.UnsupportedOperationException: count not supported on this type: Long

since nil is not a list:

> (list? nil)
false

Does nil have a special status as an empty sequence?

4 Answers 4

5

From the official documentation:

count:

Returns the number of items in the collection. (count nil) returns 0. Also works on strings, arrays, and Java Collections and Maps

So this is the spec ;)

I imagine that it ensures that any mutating value having a "countable" type can be handled at runtime.
Indeed, any reference, referencing an allowed type (strings, arrays, and Java Collections and Maps) might target nil at some point.

2
  • You're right, and the docs for first and rest also call this out as a special case. This seems inconsistent with both Lisp and Java to me. Is there some situation where this would be handy?
    – Tim Culver
    Mar 9, 2015 at 10:40
  • This behavior is not particularly inconsistent with (Common) Lisp--as Thumbnail's answer indicates. In CL nil = () = logical false (and there is no other logical false).
    – Mars
    Mar 10, 2015 at 4:16
5

there is a old lisp tradition to conflate nil and the empty list.

Now Clojure doesn't adhere to that one but in the LISP differences pages

http://clojure.org/lisps

you can read

A big difference in Clojure, is sequences. Sequences are not specific collections, esp. they are not necessarily concrete lists. When you ask an empty collection for a sequence of its elements (by calling seq) it returns nil, saying "I can't produce one". When you ask a sequence on its last element for the rest it returns another logical sequence. You can only tell if that sequence is empty by calling seq on it in turn. This enables sequences and the sequence protocol to be lazy.

Thus to be able to chain "seq" calls with many other traditional processing functions (first, rest etc.) you have to deal with nil as some kind of an empty list (this is just my understanding of the whole affair).

2
  • Can you give an example please?
    – Tim Culver
    Mar 9, 2015 at 10:50
  • 1
    (seq ()) => nil, (seq []) => nil etc.
    – noisesmith
    Mar 9, 2015 at 13:52
2

Does nil have a special status as an empty sequence?

Yes. This is called nil-punning - a Lisp tradition, as Freakhill says.

In Clojure, it only works in one direction:

  • If you supply nil where a sequence is expected, it turns itself into the empty sequence. This works in general, not just for count.

For example,

(concat nil) ; => ()

(map inc nil) ; => ()
  • But if you supply an empty sequence where nil might be expected, for example, as a logical false value, it does not convert to nil.

For example

(if () 1 2) ; => 1

(if nil 1 2) ; => 2

This page explains how Clojure, by departing from the traditional Lisp model, is able to better exploit lazy sequences.

2

Clojure has an abstraction first design (these abstractions can be protocols, interfaces, or multimethods). That is to say, that a function shouldn't generally target a specific datatype, but rather it should operate on some abstraction type, and let any datatype implement that abstraction in order to be used by that function.

Functions in Clojure that work on ordered collections should target clojure.lang.ISeq. The complication here is that we want to also target native types like String or Array or List, where we cannot add a supertype retroactively. Our solution is to use seq to get an instance of clojure.lang.ISeq. It turns out that it is convenient to treat nil as an empty ISeq, this simplifies eg. various linked list representations, as it's natural to have a nil next element for for the last element of the list, and thus to treat nil as an empty list.

1
  • So in short, Clojure does use nil-punning, but not in Clojure lists; only in the Sequence abstraction. Right?
    – Tim Culver
    Mar 11, 2015 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.