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 points are the following:
((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1))
I have also made some abstractions to retrieve the general car and cdr (for easier visibility in the code), as well as the car and cdr of the list itself.
(define (get-x p) (car p)
(define (get-y p) (car p)
(define (get-first-point pt-list)
(get-x pt-list))
(define (get-rest-points pt-list)
(get-y pt-list))
I have also already written out a generalized distance formula that I can use for any two points that I choose (keep in mind that I abstracted car and cdr to be get-x and get-y respectively).
(define (distance a b)
(if (or (null? a) (null? b))
0
(sqrt (+ (expt (- (get-x a)
(get-x b)) 2)
(expt (- (get-y a)
(get-y b)) 2)))))
Now that I have this, I know that I need to compare the various distances throughout my entire set of points and choose the maximum return value. I have a partial solution, but not one that's correct for every point.
(define (max-distance p pt-list)
(if (null? pt-list)
0
(max (distance p (get-first-point pt-list)) ; 1
(distance p (get-first-point (get-rest-points pt-list))) ; 2
(distance p (get-first-point (get-rest-points (get-rest-points pt-list)))) ; 3
(distance p (get-first-point (get-rest-points (get-rest-points (get-rest-points pt-list))))) ; 4
(distance p (get-first-point (get-rest-points (get-rest-points (get-rest-points (get-rest-points pt-list)))))) ; 5
(distance p (get-first-point (get-rest-points (get-rest-points (get-rest-points (get-rest-points (get-rest-points pt-list))))))) ; 6
)
)
)
You can probably get the gist of what I'm (horribly) attempting at doing, but this is why I need the help.