newLISP Fan Club

Forum => newLISP in the real world => Topic started by: cormullion on April 23, 2010, 10:56:31 AM

Title: Stitching lists together
Post by: cormullion on April 23, 2010, 10:56:31 AM
Given two lists, what's the easiest way to splice them together.



From this:


(set 'a '(fred jim bob))
(set 'b '(1 2 3))


to this


((fred 1) (jim 2) (bob 3))

... coming back to some newLISP coding after a few weeks away and I think I'm rusty...
Title: Re: Stitching lists together
Post by: m i c h a e l on April 23, 2010, 11:15:46 AM
This does the trick:


(map list a b)

I'm sure there's an even better way, though.



m i c h a e l
Title: Re: Stitching lists together
Post by: cormullion on April 23, 2010, 12:14:08 PM
Thanks! I've forgotten more than I thought! :)
Title: Re: Stitching lists together
Post by: Sammo on April 23, 2010, 01:53:44 PM
Another solution:
(transpose (list a b))
>((fred 1) (jim 2) (bob 3))

and applying transpose again has the effect of undoing:
(transpose (transpose (list a b)))
>((fred jim bob) (1 2 3))
Title: Re: Stitching lists together
Post by: cormullion on April 23, 2010, 11:18:56 PM
like it!
Title: Re: Stitching lists together
Post by: Tim Johnson on April 28, 2010, 08:22:27 AM
I use this function

(define (merge)(transpose (args)))

Lutz is a man of few words ...