I had a go at writing a function which interleaves two lists together:
(defun conjoin (result list1 list2)
"conjoin 2 lists. e.g. (conjoin nil '(1 2) '(3 4)) => (1 3 2 4)"
(if (eql nil list1)
result
(conjoin (nconc result
(list (car list1))
(list (car list2)))
(cdr list1) (cdr list2))))
I'm pretty sure that there must be a better way of doing things, and
that I'm reinventing a wheel. Any suggestions?
Mark Carter <··@privacy.net> writes:
> I'm pretty sure that there must be a better way of doing things, and
> that I'm reinventing a wheel. Any suggestions?
(mapcan #'list list1 list2)
--
/|_ .-----------------------.
,' .\ / | Free Mumia Abu-Jamal! |
,--' _,' | Abolish the racist |
/ / | death penalty! |
( -. | `-----------------------'
| ) |
(`-. '--.)
`. )----'
Thomas F. Burdick wrote:
> Mark Carter <··@privacy.net> writes:
>
>
>>I'm pretty sure that there must be a better way of doing things, and
>>that I'm reinventing a wheel. Any suggestions?
>
>
> (mapcan #'list list1 list2)
>
Oh dear, how embarassing for me. So many mapping functions, so little
knowledge on my part.
Many thanks.
<··@privacy.net> wrote:
> I had a go at writing a function which interleaves two lists together:
>
> (defun conjoin (result list1 list2)
> "conjoin 2 lists. e.g. (conjoin nil '(1 2) '(3 4)) => (1 3 2 4)"
> (if (eql nil list1)
> result
> (conjoin (nconc result
> (list (car list1))
> (list (car list2)))
> (cdr list1) (cdr list2))))
>
> I'm pretty sure that there must be a better way of doing things, and
> that I'm reinventing a wheel. Any suggestions?
(mapcan #'list '(1 2) '(3 4))
Has slightly different semantics than your version when the lists are
of different lengths. As a bonus it'll also work with more than two
lists:
(mapcan #'list '(1 2 3) '(a b c) '(x y z))
--
Juho Snellman
"Premature profiling is the root of all evil."
In article <·························@news.sunsite.dk>,
Mark Carter <··@privacy.net> wrote:
> I had a go at writing a function which interleaves two lists together:
>
> (defun conjoin (result list1 list2)
> "conjoin 2 lists. e.g. (conjoin nil '(1 2) '(3 4)) => (1 3 2 4)"
> (if (eql nil list1)
(eql nil list1) is virtually always written as (null list1)
> result
> (conjoin (nconc result
> (list (car list1))
> (list (car list2)))
> (cdr list1) (cdr list2))))
Please indent properly. It's impossible to tell where the end of the
NCONC is in this.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***