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

前端 未结 3 1958
有刺的猬
有刺的猬 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:36

    Compute the distance to the car of points. Either that distance is the maximum or the maximum from the cdr of points is. Result is a simple recursive algorithm traversing the list of points.

    (define (max-distance-p p points)
      (if (null? points)
          0
          (max (distance p (car points))
               (max-distance-p p (cdr points)))))
    

    The 0 is returned if you pass in points as an empty list; that might be a bit questionable. If so:

    (define (max-distance-p p points)
      (assert (not (null? points)))
      (if (null? (cdr points))
          (distance p (car points))
          (max (distance p (car points))
               (max-distance-p p (cdr points)))))
    

提交回复
热议问题