unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Sean McAfee <eefacm@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Collecting in the opposite order in a CL loop
Date: Fri, 26 Feb 2010 17:35:15 -0800	[thread overview]
Message-ID: <m2sk8nea0c.fsf@ssankar-d630rr.yojoe.local.i-did-not-set--mail-host-address--so-tickle-me> (raw)

Recently I composed this little function:

(defun digits-of (num)
  (assert (and (wholenump num) (not (zerop num))))
  (nreverse
   (loop for x = num then (/ x 10) until (zerop x) collect (mod x 10))))

It's short and sweet, but it bugs me just a little than I'm building up
a list only to immediately reverse it.  It seems to me that I ought to
be able to create the list already in the right order, but all I can
come up with so far (that uses the Common Lisp loop facility) is this:

(loop for x = num then (/ x 10) until (zerop x) with result = nil do
  (setq result (cons (mod x 10) result))
  finally return result)

That's substantially uglier than this routine that doesn't use a CL loop
at all:

(while (not (zerop x))
  (setq result (cons (mod x 10) result) x (/ x 10)))

...which I guess I could use, but I prefer to stick with the CL loop
macro when possible, if only because Emacs provides my only opportunity
to write any Common-Lisp(-like) code at all.

Is there an elegant way to build up a list "backwards" using the CL loop
facility?


             reply	other threads:[~2010-02-27  1:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-27  1:35 Sean McAfee [this message]
2010-02-27  2:42 ` Collecting in the opposite order in a CL loop Pascal J. Bourguignon
2010-02-27  3:49   ` Sean McAfee
2010-02-27 11:50     ` Pascal J. Bourguignon

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2sk8nea0c.fsf@ssankar-d630rr.yojoe.local.i-did-not-set--mail-host-address--so-tickle-me \
    --to=eefacm@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).