From: Bob Rogers <rogers-emacs@rgrjr.dyndns.org>
To: "Stephen J. Turnbull" <stephen@xemacs.org>
Cc: Tom Tromey <tromey@redhat.com>,
"Lennart Borgman \(gmail\)" <lennart.borgman@gmail.com>,
Emacs Devel <emacs-devel@gnu.org>
Subject: Re: To be a list or not
Date: Sat, 29 Dec 2007 22:52:04 -0500 [thread overview]
Message-ID: <18295.5604.556472.248446@rgrjr.rgrjr.com> (raw)
In-Reply-To: <87abnt6lym.fsf@uwakimon.sk.tsukuba.ac.jp>
From: "Stephen J. Turnbull" <stephen@xemacs.org>
Date: Sun, 30 Dec 2007 09:54:41 +0900
Tom Tromey writes:
> >>>>> "Stephen" == Stephen J Turnbull <stephen@xemacs.org> writes:
>
> Stephen> But please call it `true-list-p', which is the name of the
> Stephen> similar XEmacs built-in.
>
> Here's one stab at it. This is basically lifted from safe-length.
Looks very familiar.
XEmacs implements one change you might think about. Specifically, it
delays starting the tortoise until the length is "long enough to
suspect circularity". I don't know if the value chosen was tuned or
not. As I understand it, this makes the function slightly faster for
shorter lists, slightly slows down longer true lists, and might double
the detection time for cyclical lists.
Looks like a win to me, but YMMV.
Another trick is to unroll the loop so that it follows the tortoise, as
done in the CMU Common Lisp list-length function shown below (which is
in the public domain). If you combine both tricks, you would have two
loops, the first of which only needs to maintain a tail and a counter,
and the second only needs to maintain a tail and a tortoise.
But it's not at all clear that this is worth all that trouble . . .
-- Bob
------------------------------------------------------------------------
(defun list-length (list)
"Returns the length of the given List, or Nil if the List is circular."
(do ((n 0 (+ n 2))
(y list (cddr y))
(z list (cdr z)))
(())
(declare (fixnum n) (list y z))
(when (endp y) (return n))
(when (endp (cdr y)) (return (+ n 1)))
(when (and (eq y z) (> n 0)) (return nil))))
next prev parent reply other threads:[~2007-12-30 3:52 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-28 21:20 To be a list or not Lennart Borgman (gmail)
2007-12-28 21:40 ` Eric Hanchrow
2007-12-28 21:47 ` Nick Roberts
2007-12-28 23:05 ` Lennart Borgman (gmail)
2007-12-28 23:45 ` Nick Roberts
2007-12-29 0:18 ` Lennart Borgman (gmail)
2007-12-29 3:35 ` Bob Rogers
2007-12-29 21:11 ` Stephen J. Turnbull
2007-12-29 22:40 ` Miles Bader
2007-12-29 22:44 ` Tom Tromey
2007-12-29 23:22 ` Andreas Schwab
2007-12-31 16:38 ` Tom Tromey
2007-12-31 17:13 ` Andreas Schwab
2007-12-30 0:54 ` Stephen J. Turnbull
2007-12-30 3:52 ` Bob Rogers [this message]
2007-12-30 1:37 ` Richard Stallman
2007-12-29 17:49 ` Richard Stallman
2007-12-29 13:51 ` Richard Stallman
2007-12-29 14:30 ` Thien-Thi Nguyen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=18295.5604.556472.248446@rgrjr.rgrjr.com \
--to=rogers-emacs@rgrjr.dyndns.org \
--cc=emacs-devel@gnu.org \
--cc=lennart.borgman@gmail.com \
--cc=stephen@xemacs.org \
--cc=tromey@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.