Applying multiple lists?

Q&A's, tips, howto's
Locked
methodic
Posts: 58
Joined: Tue May 10, 2005 5:04 am

Applying multiple lists?

Post by methodic »

Hi there,

Having a brain-fart, I blame it on dropping my morning coffee by accident before finishing it.

I have a list with multiple lists inside, and I'd like to map a function to each list as if I were passing them separately to map.

Example:
(map append '("one" "two") '("for" "you")) will render ("onefor" "twoyou").

I'd like to accomplish that, but if the argument to map is one list, with multiple lists. Sort of like this psuedo-code:
(map append (map each mylist))

Of course I could write a primitive function called "each" that did nothing but return a single element, but I figured that would be taking the long road since I'm sure newLisp has something built in I'm missing.

Thanks in advance!

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Re: Applying multiple lists?

Post by Kazimir Majorinc »

(eval (extend '(map append) (map quote '(("one" "two")("for" "you")))))
(map (fn(x)(apply append x)) (transpose '(("one" "two")("for" "you"))))

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

Re: Applying multiple lists?

Post by cormullion »

If transpose works for you, then it's good"

Code: Select all

(set 'l '(("one" "two") ("for" "you")))
(map join (transpose l))
;-> ("onefor" "twoyou")

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

Re: Applying multiple lists?

Post by cormullion »

Although - after a coffee - I thought that

Code: Select all

(set 'L '(("one" "two") ("for" "you")))
    (map append (L 0) (L 1))
)
was easier if you knew there were just two elements..

methodic
Posts: 58
Joined: Tue May 10, 2005 5:04 am

Re: Applying multiple lists?

Post by methodic »

cormullion wrote:Although - after a coffee - I thought that

Code: Select all

(set 'L '(("one" "two") ("for" "you")))
    (map append (L 0) (L 1))
)
was easier if you knew there were just two elements..
Bah, transpose is what I was looking for, thanks! Here is what I was looking to do:
I had a list of lists of scores, and wanted to add all scores with their respective index:

Code: Select all

(map (fn (x) (apply add x)) (map append (transpose scores)))

Locked