sicp每日一题[2.48]

think2times / 2024-10-23 / 原文

Exercise 2.48

A directed line segment in the plane can be represented as a pair of vectors—the vector running from the origin to the start-point of the segment, and the vector running from the
origin to the end-point of the segment. Use your vector representation from Exercise 2.46 to define a representation for segments with a constructor make-segment and selectors
start-segment and end-segment.


这道题太简单了,没啥说的。

(define (make-segment v1 v2)
  (cons v1 v2))

(define (start-segment segment)
  (car segment))

(define (end-segment segment)
  (cdr segment))


(define v1 (make-vect 3 4))
(define v2 (make-vect -4 3))
  
(define a-segment (make-segment v1 v2))
a-segment
(start-segment a-segment)
(end-segment a-segment)

; 执行结果
(mcons (mcons 3 4) (mcons -4 3))
(mcons 3 4)
(mcons -4 3)