Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector addition of lists

Tags:

lisp

scheme

If I had a N lists each of length M, how could I write a nice clean function to return a single list of length M, where each element is the sum of the corresponding elements in the N lists?

(starting to learn lisp - go easy!)

like image 258
ntimes Avatar asked Dec 13 '22 23:12

ntimes


1 Answers

This is a job for the map and apply functions. Here is a way to do it, with an EDIT suggested by Nathan Sanders:

(define (add-lists . more)
  (apply map + more))
like image 93
Pillsy Avatar answered Jan 12 '23 00:01

Pillsy