Stitching lists together

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Stitching lists together

Post by cormullion »

Given two lists, what's the easiest way to splice them together.

From this:

Code: Select all

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

Code: Select all

((fred 1) (jim 2) (bob 3))
... coming back to some newLISP coding after a few weeks away and I think I'm rusty...

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Stitching lists together

Post by m i c h a e l »

This does the trick:

Code: Select all

(map list a b)
I’m sure there’s an even better way, though.

m i c h a e l

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Stitching lists together

Post by cormullion »

Thanks! I've forgotten more than I thought! :)

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Re: Stitching lists together

Post by Sammo »

Another solution:

Code: Select all

(transpose (list a b))
>((fred 1) (jim 2) (bob 3))
and applying transpose again has the effect of undoing:

Code: Select all

(transpose (transpose (list a b)))
>((fred jim bob) (1 2 3))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Stitching lists together

Post by cormullion »

like it!

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Stitching lists together

Post by Tim Johnson »

I use this function

Code: Select all

(define (merge)(transpose (args)))
Lutz is a man of few words ...
Programmer since 1987. Unix environment.

Locked