[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LIST-LENGTH & circular lists

This page is part of the web mail archives of SRFI 1 from before July 7th, 2015. The new archives for SRFI 1 contain all messages, not just those from before July 7th, 2015.

   From: Doug Currie <e@xxxxxxxxxxx>
   One thing I have found useful is a version of list-length that terminates
   on circular-lists, perhaps returning #f or 0 (or -N) for circular lists. It
   kills two birds with one stone, and isn't much harder to implement than

We could extend LIST-LENGTH to return #F on circular lists.

This does have the effect of screwing up type inference on LIST-LENGTH -- you
can no longer conclude that it returns a fixnum.

Alternately, I could allow LIST-LENGTH to continue to diverge or cause an
error when applied to circular lists, and introduce a new function with the
fancy behaviour. Some people might not like the fact that they will now have
to throw in false-tests after their calls to LIST-LENGTH if they need to rule
out circular lists. What would be a good name for the variant that handled
circular lists?  I thought up
    LIST-LENGTH? 	(Some won't like this as it returns non-#t true vals.)
    LIST-LENGTH+	("+" for "extra" version)
    FINITE-LIST		(Don't like it -- looks like a constructor to me.)
    FINITE-LIST?	(Some won't like this as it returns non-#t true vals.)
I'm not really fond of any of these names, but among them, would choose

My preference would be to add a new function, named LIST-LENGTH+, and
allow LIST-LENGTH to diverge or report an error on circular lists.
But it's a mild preference.