我目前正在尝试从点列表中编写一个函数,该函数返回从点 p 到我的点列表中离 p 最远的点的距离。我的要点如下:
((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1))
我还做了一些抽象来检索一般的 car 和 cdr(为了在代码中更容易看到),以及列表本身的 car 和 cdr。
(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))
我还写了一个通用距离公式,我可以将它用于我选择的任何两个点(请记住,我将 car 和 cdr 分别抽象为 get-x 和 get-y)。
(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)))))
现在我有了这个,我知道我需要比较整个点集的各种距离并选择最大的返回值。我有一个部分解决方案,但不是一个对每一点都正确的解决方案。
(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
)
)
)
您可能可以了解我正在(可怕地)尝试做的事情的要点,但这就是我需要帮助的原因。