Re: newbie question
- From: Frank Buss <fb@xxxxxxxxxxxxx>
- Date: Sat, 31 Dec 2005 21:54:19 +0100
dvlfrnd wrote:
> (a b c d e f ......)
>
> how do i turn this into
> ((a b) (c d) (e f)....)
you could use loop for it, one of my favorite Lisp macros, like described
in the other postings.
> then i tried this
>
> (defun pair-add-element (n lst)
> (mapcar #'(lambda (x) (list n x)) lst))
>
> if i could split the list into two,
> where the original is (a b c d e f)
> to
> (a c e) and (b d f)
>
> i could mapcar these two new lists using pair-add-element function
> above
> and return this
> ((a b) (c d) (e f))
your pair-add-element pairs every elemnt of the list "lst" with one element
"n". I don't understand how this would help. But mapcar itself can be used
for this, becaue it accepts multiple lists:
(mapcar #'(lambda (x y) (list x y)) '(a b) '(1 2)) -> ((A 1) (B 2))
> 1- how do i split a list into two, taking even'th and odd'th elemtns
> into separate lists
I'm sure there are more elegant solutions with car, cdr and recursive
functions, but again some loop magic:
(defun partition (list)
(loop for (i j) on list by (function cddr)
finally (return (values odd even))
collect i into odd
collect j into even))
(defun partition-pairs (list)
(multiple-value-bind (odd even) (partition list)
(mapcar #'(lambda (x y) (list x y)) odd even)))
or not much longer:
(defun partition-pairs (list)
(multiple-value-bind (odd even) (partition list)
(loop for x in odd for y in even collect (list x y))))
--
Frank Buss, fb@xxxxxxxxxxxxx
http://www.frank-buss.de, http://www.it4-systems.de
.
- References:
- newbie question
- From: dvlfrnd
- newbie question
- Prev by Date: Re: newbie question
- Next by Date: Re: newbie question
- Previous by thread: Re: newbie question
- Next by thread: Re: newbie question
- Index(es):
Relevant Pages
|
|