djpeebles
djpeebles

Reputation: 71

Computing functions over multiple lists

I need to produce a list containing the averages of a lists of lists where the number of sub-lists could vary. So given the input list:

((l1a l1b l1c) (l2a l2b l2c) (l3a l3b l3c)...)

the output would be:

(average(l1a l2a l3a) average(l1b l2b l3b) average(l1c l2c l3c)...).

I'm sure there's a really elegant way to do this in lisp but I don't know where to start. Any advice would be gratefully received.

Upvotes: 2

Views: 98

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139401

CL-USER 27 > (let* ((list-of-lists '((1.0 2.0 3.0)
                                     (1.0 3.0 5.0)
                                     (1.0 4.0 6.0)
                                     (1.0 5.0 8.0)))
                    (length (length list-of-lists)))
               (mapcar (lambda (x)
                         (/ x length))
                       (reduce (lambda (l1 l2)
                                 (mapcar #'+ l1 l2))
                               list-of-lists)))
(1.0 3.5 5.5)

Upvotes: 6

Related Questions