I\'m currently trying to write a function from a list of points that returns the distance from a point p to a point in my point list that is farthest away from p. My list of poi
This is a case where you want to fold a function f over your list of points. The function f should take a distance d and point x and return the maximum d and the distance between x and your special designated point p. I've described folds in a bit more detail in some other answers:
The main point is that in a fold, you take a function, an initial value, and a list. You apply the function to the first element of the list and the initial value to produce some new value. Now you recursively fold with the the same function, the new value, and the rest of the list. This is essentially a loop like this:
current value = initial value
while list is not empty
current value = result of function called with first element of list and current value
list = rest of list
return current value
In Scheme, where tail call optimization is guaranteed, it is that loop. In your case, you just need to determine what the initial value should be, and what the function should be. Since distances are always non-negative, a reasonable initial value is zero. The function is a bit more complicated. current value will be a distance, but the first element of list will be a point. The result of the function needs to be a new distance, and the new distance should be the maximum of the distance current value and the distance between some the point from list and the special point. In Racket, this kind of fold is called foldl, so we end up with something like this:
(define special-points '((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1)))
(define (distance a b)
(let ((square (lambda (x)
(* x x))))
(sqrt (+ (square (- (car a) (car b)))
(square (- (cdr a) (cdr b)))))))
(define (max-distance point points)
(foldl (lambda (current-point current-distance)
(max current-distance
(distance point current-point)))
0
points))
> (max-distance '(4 . 12) special-points)
13.601470508735444
> (max-distance '(8 . 8) special-points)
13.0
> (max-distance '(2 . 5) special-points)
7.615773105863909
If you're not using Racket, you'll have to write your own foldl
, but it's really not very hard. (This actually isn't quite as sophisticated as Racket's foldl
, which can take any positive number of lists, but it will work for this case.)
(define (foldl function initial-value list)
(if (null? list)
initial-value
(foldl function
(function (car list) initial-value)
(cdr list))))