Discussion:
Predicate for true lists
(too old to reply)
Basil L. Contovounesios
2018-04-16 19:34:00 UTC
Permalink
A while ago, I added a little convenience function to my init file[1]
to determine whether a given object is a true list (as opposed to a
circular or dotted one):

(null (nthcdr (safe-length object) object))

[1]: https://github.com/basil-conto/dotfiles/blob/96aeb904a6fd94d9fbcf95483fd4f79194e90592/.emacs.d/lisp/blc-lib.el#L46-L49

Since then, I have noticed variations of this predicate/condition strewn
around the Emacs sources, albeit usually implemented less efficiently.

Would such a predicate be a welcome addition to, say, subr.el or
subr-x.el? I attach a first draft of a patch targeting subr.el for your
consideration.

Also attached is a toy benchmark comparing the new function list-true-p
with the existing format-proper-list-p and ert--proper-list-p when
given, in turn, a true, dotted, and circular list as argument.
It prints the following, after a couple of runs:

‘format-proper-list-p’
true (0.16966186900000002 0 0.0)
dotted (0.168859839 0 0.0)
circular (0.244791363 0 0.0)
‘ert--proper-list-p’
true (0.622797443 0 0.0)
dotted (0.621622385 0 0.0)
circular (0.9150398590000001 0 0.0)
‘list-true-p’
true (0.042970005000000006 0 0.0)
dotted (0.04294060500000001 0 0.0)
circular (0.057346661 0 0.0)

P.S. What is the preferred way of formatting car/cdr in docstrings?
The manuals seem to use small caps CAR/CDR and subr.el seems to
alternate between no quotes and `car'/`cdr'. I have gone with the
latter for the docstring of list-true-p, at least for now.

Thanks,
--
Basil
Loading...