all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Simple e-lisp question
@ 2009-04-14 19:49 Eric Lilja
  2009-04-14 20:39 ` Drew Adams
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Lilja @ 2009-04-14 19:49 UTC (permalink / raw)
  To: help-gnu-emacs

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.





^ permalink raw reply	[flat|nested] 4+ messages in thread
[parent not found: <mailman.5349.1239738617.31690.help-gnu-emacs@gnu.org>]

end of thread, other threads:[~2009-04-14 22:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-14 19:49 Simple e-lisp question Eric Lilja
2009-04-14 20:39 ` 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

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.