0

I'm trying to write a procedure that returns the longest distance between the farthest two points in a list. My list is

((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1))

And i've already written a distance procedure and one that returns the maximum distance between two points:

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

Now, I just need to write something that only returns the largest value obtained by taking two of those points. What I have so far is

 (define (max-range pt-list)
     (if (null? pt-list)
         0
         (max (max-distance (car pt-list) (pt-list))
              (max-range (cdr pt-list)))))

However, when i run the test case (display+ (max-range my-pt-list)) i get the error: The object ((0 . -1)) is not applicable, which leads me to believe that it is working up until it reaches the last point.

4

2 回答 2

1

问题出在这一行中max-range

(max-distance (car pt-list) (pt-list))

它应该是:

(max-distance (car pt-list) pt-list)

请注意,您必须传递列表,而不是应用它(它不是函数)。这就是错误所指向的。

于 2013-10-23T21:44:54.047 回答
0

我认为您忽略了使用cdr. 在您max-range调用的函数max-distance中-该调用的参数应该是carand cdrof pt-list。像这样:

(define (max-range pt-list)
  (if (null? pt-list)
      0
      (max (max-distance (car pt-list) (cdr pt-list))
           (max-range (cdr pt-list)))))

您使用cdr第二个参数是因为没有理由重新计算距离(car pt-list)(它将为零,永远不会是最大值)。

于 2013-10-23T22:31:04.487 回答