Reputation: 75
So I have an assignment with the following criteria:
The definition of a function named euclidean-distance is given. This function computes the distance between two points in the xy-plane. The points are given as four separate numbers: x1, y1, x2, and y2.
Rewrite the function so that it takes two arguments, both of which are of type Posn, and run the same computation.
> (define the-origin (make-posn 0 0))
> (define some-point (make-posn 3 7))
> (euclidean-distance the-origin some-point)
#i7.615773105863909
> (euclidean-distance (make-posn 1 1) (make-posn 4 5))
5
My trouble here is that I'm not sure how to extract the information I need in order to compare square the differences and such. What I have so far:
(define (euclidean-distance posn1 posn2)
(sqrt (+ (sqr (- posn1-x posn2-x))
(sqr (- posn1-y posn2-y)))))
Not sure how to go about what I need to do.
Upvotes: 1
Views: 709
Reputation: 236112
Just use the accessor procedures of each position, like this:
(define (euclidean-distance posn1 posn2)
(sqrt (+ (sqr (- (posn-x posn1) (posn-x posn2)))
(sqr (- (posn-y posn1) (posn-y posn2))))))
Upvotes: 3