From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Lilja Newsgroups: gmane.emacs.help Subject: Simple e-lisp question Date: Tue, 14 Apr 2009 21:49:52 +0200 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1239738773 17811 80.91.229.12 (14 Apr 2009 19:52:53 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 14 Apr 2009 19:52: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 Tue Apr 14 21:54:13 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 1Ltoft-0006Qm-Ot for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 21:52:06 +0200 Original-Received: from localhost ([127.0.0.1]:51805 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LtoeU-0008Jw-Q0 for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 15:50:38 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ltoe8-0008Jh-Eg for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 15:50:16 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ltoe3-0008I5-2C for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 15:50:15 -0400 Original-Received: from [199.232.76.173] (port=39169 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ltoe2-0008I2-TM for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 15:50:10 -0400 Original-Received: from main.gmane.org ([80.91.229.2]:33882 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Ltoe2-00016U-G4 for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 15:50:10 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1Ltodz-000731-FN for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 19:50:07 +0000 Original-Received: from h-60-61.a163.priv.bahnhof.se ([79.136.60.61]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 14 Apr 2009 19:50:07 +0000 Original-Received: from mindcooler by h-60-61.a163.priv.bahnhof.se with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 14 Apr 2009 19:50:07 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 23 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: h-60-61.a163.priv.bahnhof.se User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) 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:63739 Archived-At: 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. 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? - Eric Lilja PS. I don't know lisp, heh, just found this old exercise in my papers. DS.