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: Simple e-lisp question Date: Wed, 15 Apr 2009 00:07:45 +0200 Organization: Informatimago Message-ID: <878wm2j3da.fsf@galatea.local> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1239748852 18123 80.91.229.12 (14 Apr 2009 22:40:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 14 Apr 2009 22:40:52 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Apr 15 00:42:11 2009 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.50) id 1LtrKP-00011q-Gf for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Apr 2009 00:42:05 +0200 Original-Received: from localhost ([127.0.0.1]:43636 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LtrJ0-0004cx-Ub for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 18:40:39 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!cleanfeed4-a.proxad.net!nnrp13-1.free.fr!not-for-mail Original-Newsgroups: gnu.emacs.help 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.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) Cancel-Lock: sha1:MjA2Y2NjMjIzZTZiMDU4YmJiZTg0NDkzYTQ1YjY4YWZhZjE2NGZkZg== Original-Lines: 67 Original-NNTP-Posting-Date: 15 Apr 2009 00:07:47 MEST Original-NNTP-Posting-Host: 88.182.134.169 Original-X-Trace: 1239746867 news-4.free.fr 18773 88.182.134.169:61158 Original-X-Complaints-To: abuse@proxad.net Original-Xref: news.stanford.edu gnu.emacs.help:168467 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:63742 Archived-At: Eric Lilja writes: > Hi, I want to write an elisp-function switch2 that should switch the > first two elements in a list. I came up with this: > > (defun switch2 (x) > (append (list (second x) (first x)) (nthcdr 2 x)) > ) > (switch2 '(a b c d)) ; Yields (b a c d) > (switch2 '(a b)) ; Yields (b a) > (switch2 '(a)) ; Yields (nil a) > (switch2 '()) ; Yields (nil nil) > > The problem is how it handles a list with only one element and an > empty list. I'm not sure how it should handle only one element, maybe > return an unmodified list or an empty list? If an empty list is given > the result should be an empty list. This is not a lisp question then. It's a make up your mind question. > How can I fix my swith2 to cope better with the last two calls above > and can I use the more fundamental list functions if you know what I > mean and avoid nthcdr altogether? So what do you want? On my part, I'd like it to return the object unmodified if there are less than two elements or it's not a list. These kinds of operations are usually named "swap". Switch means nothing in this context. You can switch things on or off, but here you want to swap. (defun swap-first-two (object) (cond ((atom object) object) ((atom (rest object)) object) (t (list* (second object) (first object) (rest (rest object)))))) (defun test/swap-first-two () (equalp '( a () (a) (a . b) (b a) (b a . c) (b a c) (b a c d) ) (mapcar (function swap-first-two) '( a () (a) (a . b) (a b) (a b . c) (a b c) (a b c d) )))) (test/swap-first-two) --> t -- __Pascal Bourguignon__