[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Simple e-lisp question
From: |
Eric Lilja |
Subject: |
Simple e-lisp question |
Date: |
Tue, 14 Apr 2009 21:49:52 +0200 |
User-agent: |
Thunderbird 2.0.0.21 (Windows/20090302) |
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.
- Simple e-lisp question,
Eric Lilja <=