Vector Add Lists

If I had N lists of each 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 lists of N?

(starting to learn lisp is easy!)

+4
source share
3 answers

This job is for map and apply functions. Here is a way to do this using EDIT proposed by Nathan Sanders:

 (define (add-lists . more) (apply map + more)) 
+9
source

For more syntax like matlab:

 (define (piecewise func) (lambda more (apply map func more))) (define pw piecewise) ((pw +) '(1 2 3 4 5) '(6 7 8 9 0)) ((pw -) '(1 2 3 4 5) '(6 7 8 9 0)) ((pw *) '(1 2 3 4 5) '(6 7 8 9 0)) ((pw /) '(1 2 3 4 5) '(6 7 8 9 0.1)) 

outputs:

 (7 9 11 13 5) (-5 -5 -5 -5 5) (6 14 24 36 0) (1/6 2/7 3/8 4/9 50.0) 
+2
source

Only this works in the MIT circuit.

  (map + '(1 2 3) '(4 5 6) '(7 8 9)) ;Value 28: (12 15 18) 
+2
source

Source: https://habr.com/ru/post/1304164/


All Articles