5

I find it surprising that rings (from make-ring) are sequences (satisfy sequencep), but cannot be operated on by standard sequence functions (length, elt, etc.).

What, then, does being a "sequence" entail? The Elisp manual says that:

The “sequence” type is the union of two other Lisp types: lists and arrays. In other words, any list is a sequence, and any array is a sequence. The common property that all sequences have is that each is an ordered collection of elements.

The diagram in the manual does not include rings. Should rings really be considered sequences?

Tianxiang Xiong
  • 3,848
  • 16
  • 27

1 Answers1

4

Predicate sequencep returns non-nil if its argument is a list or an array, and in this case "list" includes a cons cell (a dotted pair). (sequencep '(a . b)) returns t.

(setq foo  (make-ring 3))

C-h v foo shows that foo is (0 0 . [nil nil nil]).

So a ring is a sequence - because it is a cons, but a sequence is not necessarily a ring.

Whether sequencep should behave this way in Emacs Lisp is a good question. It does not correspond to what Common Lisp considers a sequence, for instance. In Common Lisp only proper lists (with nil as the last cdr) are sequences, not dotted lists.

You have function ring-convert-sequence-to-ring, which returns a ring that corresponds to its sequence argument.

Drew
  • 75,699
  • 9
  • 109
  • 225