From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Collecting in the opposite order in a CL loop Date: Sat, 27 Feb 2010 03:42:02 +0100 Organization: Informatimago Message-ID: <874ol3bds5.fsf@galatea.lan.informatimago.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1267242053 6269 80.91.229.12 (27 Feb 2010 03:40:53 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 27 Feb 2010 03:40:53 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Feb 27 04:40:49 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1NlDXs-0006PW-IG for geh-help-gnu-emacs@m.gmane.org; Sat, 27 Feb 2010 04:40:48 +0100 Original-Received: from localhost ([127.0.0.1]:36257 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NlDXr-0001Wl-UT for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Feb 2010 22:40:47 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 90 Original-X-Trace: individual.net sPA7mSSXa9I/L8wuwYBIEAFsC3eDqU0dv4uxsitxqaaASpR05B Cancel-Lock: sha1:MmYzOTNlOGUzNTI5OTJhNmNhMTRmZDM0OTcxMWZhMTJkNzA0OGM1Yg== sha1:AEfiLVyOjgvXyszFHSS98wIaEOI= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (darwin) Original-Xref: news.stanford.edu gnu.emacs.help:177143 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:72178 Archived-At: Sean McAfee writes: > 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) Use push instead of setq cons. Use truncate instead of / for integer division. Use finally (return ...); finally return is ClTl2, not Common Lisp. (loop with result = '() for x = num then (truncate x 10) until (zerop x) do (push (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? Notice that building this list backwards as you want it is wrong: 1234 --> (1 2 3 4) 34 --> (3 4) with the most significant digits in the lowest indexes, you cannot use the list of digits do to anything. If you kept them in the right order, with least significant digits in lowest indexes: 1234 --> (4 3 2 1) 34 --> (4 3) you can easily implement arithmetic operators, or comparisons, or whatever you need to do with them. (defun collect-digits (n) (unless (zerop n) (cons (mod n 10) (collect-digits (truncate n 10))))) (collect-digits 1234) --> (4 3 2 1) (collect-digits -1234) --> (6 7 8 9) ; 10-complement (defun accumulate-digits (n d) (if (zerop n) d (accumulate-digits (truncate n 10) (cons (mod n 10) d)))) (accumulate-digits 1234 '()) --> (1 2 3 4) (accumulate-digits -1234 '()) --> (9 8 7 6) ; 10-complement -- __Pascal Bourguignon__