From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: Simple e-lisp question Date: Tue, 14 Apr 2009 13:39:59 -0700 Message-ID: <002f01c9bd41$31391c00$0200a8c0@us.oracle.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1239741619 28159 80.91.229.12 (14 Apr 2009 20:40:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 14 Apr 2009 20:40:19 +0000 (UTC) To: "'Eric Lilja'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Apr 14 22:41:36 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 1LtpRi-0007SD-6V for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 22:41:30 +0200 Original-Received: from localhost ([127.0.0.1]:36537 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LtpQJ-0005le-H3 for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 16:40:03 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LtpPy-0005ko-Th for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 16:39:42 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LtpPu-0005i9-4I for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 16:39:42 -0400 Original-Received: from [199.232.76.173] (port=55903 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LtpPt-0005i6-W5 for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 16:39:38 -0400 Original-Received: from acsinet11.oracle.com ([141.146.126.233]:34600) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LtpPt-0007yV-HM for help-gnu-emacs@gnu.org; Tue, 14 Apr 2009 16:39:37 -0400 Original-Received: from acsinet13.oracle.com (acsinet13.oracle.com [141.146.126.235]) by acsinet11.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id n3EKeptv009320 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 14 Apr 2009 20:40:52 GMT Original-Received: from acsmt704.oracle.com (acsmt704.oracle.com [141.146.40.82]) by acsinet13.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id n3EKdxxl016311; Tue, 14 Apr 2009 20:40:00 GMT Original-Received: from dradamslap1 (/141.144.161.223) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 14 Apr 2009 13:39:29 -0700 X-Mailer: Microsoft Office Outlook 11 Thread-Index: Acm9Om62b4LgAPvZRRSAE/pF0beEzwAAUD4g X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 In-Reply-To: X-Source-IP: acsmt704.oracle.com [141.146.40.82] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A09020B.49E4F483.0180:SCFMA4539814,ss=1,fgs=0 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 1) 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:63740 Archived-At: > switch the first two elements in a list. > > (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? Lisp is English. If you can describe it clearly, you can code it. If x has at least 2 elements, then 2nd, 1st, 3rd... Else x at least 2 = cdr 2nd = cadr 1st = car 3rd... = cddr (defun switch2 (x) (if (cdr x) (cons (cadr x) (cons (car x) (cddr x))) lst)) Hope this wasn't your only homework. ;-)