Author Topic: Total of list in list  (Read 1470 times)

0 Members and 1 Guest are viewing this topic.

Woabow

  • Newt
  • Posts: 56
Total of list in list
« on: September 17, 2012, 12:46:26 PM »
Actually I think I should be able to solve this myself, but I'm running out of time.

With this code all items in two lists are added:
Code: [Select]
(mapcar '+ '(1 2 3 4 5) '(1 2 3 4 5))
But I would like to add all items of lists in a list:
Code: [Select]
((1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5))
to get:
Code: [Select]
(4 8 12 16 25)
I'm sure it must be easy, but please help me out. The lists in the list are always the same length.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Total of list in list
« Reply #1 on: September 17, 2012, 12:47:43 PM »
Code: [Select]
(apply 'mapcar (cons '+ '((1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5))))

Woabow

  • Newt
  • Posts: 56
Re: Total of list in list
« Reply #2 on: September 17, 2012, 02:39:59 PM »
Thanks Lee, appreciated.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Total of list in list
« Reply #3 on: September 17, 2012, 05:47:12 PM »
You're welcome  :-)