Answer :
Answer:
Explanation:
Solution:
-The task is more likely to compute the distance of n-dimensional vectors, i.e.
sqrt((x1-y1)^2 + ... + (xn-yn)^2).
- With a recursive version, I consider that p and q are two mathematical vectors, so that p contains different coordinates (p1, ..., pn), which differs from your implementation where p contains all x's and q all y's.
- So, you have to compute (pi - qi)^2 for each for pair (pi, qi) of elements taken in parallel from p and q, sum the intermediate values and take the square root.
- The function would be as follows:
" (defun euclidean-distance-it (p q)
(destructuring-bind (x1 x2) p
(destructuring-bind (y1 y2) q
(sqrt (+ (expt (- x1 y1) 2)
(expt (- x2 y2) 2)))))) "
- With high-order functions, you don't even need to use recursion. The function would be:
" (defun distance (p q)
(sqrt
(reduce #'+
(map 'list
(lambda (px qx) (expt (- px qx) 2))
p q)))) "