all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eric Lilja <mindcooler@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Simple e-lisp question
Date: Tue, 14 Apr 2009 21:49:52 +0200	[thread overview]
Message-ID: <gs2pd2$gte$1@ger.gmane.org> (raw)

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.





             reply	other threads:[~2009-04-14 19:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-14 19:49 Eric Lilja [this message]
2009-04-14 20:39 ` Simple e-lisp question Drew Adams
2009-04-14 20:54   ` Eric Lilja
     [not found] <mailman.5349.1239738617.31690.help-gnu-emacs@gnu.org>
2009-04-14 22:07 ` 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

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

  git send-email \
    --in-reply-to='gs2pd2$gte$1@ger.gmane.org' \
    --to=mindcooler@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.