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: | 327 |
Nodes: | 16 (3 / 13) |
Uptime: | 37:39:42 |
Calls: | 7,251 |
Calls today: | 10 |
Files: | 12,586 |
Messages: | 5,557,657 |
Posted today: | 1 |