Combining Map and Reduce

A combination of map and reduce can provide a great deal of power in a compact form.

The Euclidean distance between two points in n-space is the square root of the sum of squares of the differences between the points in each dimension.

Using map and reduce, we can define Euclidean distance compactly for any number of dimensions:

(defun edist (pointa pointb)
  (sqrt (reduce '+
          (mapcar 'square
                  (mapcar '- pointa pointb)))) )

>(edist '(3) '(1))


>(edist '(3 3) '(1 1))


>(edist '(3 4 5) '(2 4 8))


Contents    Page-10    Prev    Next    Page+10    Index