Re: newbie question



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
.



Relevant Pages

  • Re: Uninterned symbols at macroexpansion time.
    ... (loop for somevar in '(some list here) ... so if we give the macro a gensym in ... collect (BACKQUOTE (UNQUOTE X)))) ... simple type (meaning eg symbols, lists, etc) not ...
    (comp.lang.lisp)
  • Re: Learning Lisp in Linux?
    ... To adapt the language to the task at hand? ... unless (member key excluded-keys) ... (loop (cddr property-list) ... correct property lists as results. ...
    (comp.lang.lisp)
  • Re: Replacing subsequences
    ... >> different substring, of a different length (for quoting parameters to SQL ... fill-pointer that let me append sequences to my sequence at the fill ... Worth the price of avoiding loop, ... Is there anything besides lists and vectors to worry about? ...
    (comp.lang.lisp)
  • Re: loop: finally collect
    ... | Is it possible to avoid such a `finally' clause but still using a loop ... collect element into tmp-collection ... clause to finalize the result and successfully obscures what you seem ... Exactly how long are these lists?! ...
    (comp.lang.lisp)
  • Re: Another "gotta be a better way" from The Real World of Lisp
    ... (defun my-tree-ify (data) ... (loop with r;; loop for the fun of it ... (loop for lists = data then (cdr lists) ... "Algebra is the metaphysics of arithmetic." ...
    (comp.lang.lisp)