* 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
* RE: Simple e-lisp question
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
0 siblings, 1 reply; 4+ messages in thread
From: Drew Adams @ 2009-04-14 20:39 UTC (permalink / raw)
To: 'Eric Lilja', help-gnu-emacs
> 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. ;-)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Simple e-lisp question
2009-04-14 20:39 ` Drew Adams
@ 2009-04-14 20:54 ` Eric Lilja
0 siblings, 0 replies; 4+ messages in thread
From: Eric Lilja @ 2009-04-14 20:54 UTC (permalink / raw)
To: help-gnu-emacs
Drew Adams wrote:
>
> 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. ;-)
Thanks alot Mr Adams! And it wasn't even homework, just an old exercise
I found.
- EL
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Simple e-lisp question
[not found] <mailman.5349.1239738617.31690.help-gnu-emacs@gnu.org>
@ 2009-04-14 22:07 ` Pascal J. Bourguignon
0 siblings, 0 replies; 4+ messages in thread
From: Pascal J. Bourguignon @ 2009-04-14 22:07 UTC (permalink / raw)
To: help-gnu-emacs
Eric Lilja <mindcooler@gmail.com> writes:
> 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.
This is not a lisp question then. It's a make up your mind question.
> 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?
So what do you want?
On my part, I'd like it to return the object unmodified if there are
less than two elements or it's not a list.
These kinds of operations are usually named "swap". Switch means
nothing in this context. You can switch things on or off, but here
you want to swap.
(defun swap-first-two (object)
(cond
((atom object) object)
((atom (rest object)) object)
(t (list* (second object) (first object) (rest (rest object))))))
(defun test/swap-first-two ()
(equalp '( a
()
(a)
(a . b)
(b a)
(b a . c)
(b a c)
(b a c d) )
(mapcar (function swap-first-two)
'( a
()
(a)
(a . b)
(a b)
(a b . c)
(a b c)
(a b c d) ))))
(test/swap-first-two) --> t
--
__Pascal Bourguignon__
^ permalink raw reply [flat|nested] 4+ messages in thread
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
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).