Consider n vectors (or lists) v1, ..., vn of equal length, an n-
variate function f, and a bivariate function g.
I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
(reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
but without the intermediate vector (my vectors are long). I can of
course write a function to do this, but I am wondering if there is a
clever way to do it with CL library functions.
I don't think there is a way to do this sort of thing directly. It's
not difficult (or even particularly ugly) to do using LOOP, though. For
the case G = #'+, it's particularly nice:
(loop for x across first-vector
for y across second-vector
sum (funcall f x y))
but for general G, you need
(loop for x across first-vector
for y across second-vector
for temp = (funcall f x y)
for result = temp then (funcall result temp)
finally (return result))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 388 |
Nodes: | 16 (2 / 14) |
Uptime: | 10:31:35 |
Calls: | 8,221 |
Files: | 13,122 |
Messages: | 5,872,709 |