From: "Drew Adams" <drew.adams@oracle.com>
To: "'Eric Lilja'" <mindcooler@gmail.com>, <help-gnu-emacs@gnu.org>
Subject: RE: Simple e-lisp question
Date: Tue, 14 Apr 2009 13:39:59 -0700 [thread overview]
Message-ID: <002f01c9bd41$31391c00$0200a8c0@us.oracle.com> (raw)
In-Reply-To: <gs2pd2$gte$1@ger.gmane.org>
> 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. ;-)
next prev parent reply other threads:[~2009-04-14 20:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-14 19:49 Simple e-lisp question Eric Lilja
2009-04-14 20:39 ` Drew Adams [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='002f01c9bd41$31391c00$0200a8c0@us.oracle.com' \
--to=drew.adams@oracle.com \
--cc=help-gnu-emacs@gnu.org \
--cc=mindcooler@gmail.com \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).