Finding maximum distance between two points in a list (scheme)

前端 未结 3 1902
有刺的猬
有刺的猬 2021-01-28 09:49

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

3条回答
  •  一整个雨季
    2021-01-28 10:22

    get-yshould use cdr, not car.

    get-x and get-y miss a closing parenthesis.

    For max-distance, I'd go for

    (define (max-distance p pt-list)
      (apply max (map (lambda (x) (distance p x)) pt-list)))
    

    which means that you don't need get-first-point and get-rest-points.

    Illustration (using (1 . 1) as p):

    > (map (lambda (x) (distance '(1 . 1) x)) '((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1)))
    '(3.1622776601683795 5.385164807134504 11.704699910719626 5.385164807134504 10.897706180660222 2.23606797749979)
    
    > (apply max (map (lambda (x) (distance '(1 . 1) x)) '((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1))))
    11.704699910719626
    

    Depending on your definition of "farthest" you may want to include abs in the expression.

提交回复
热议问题