欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SICP 2.2 习题答案

程序员文章站 2022-07-14 10:55:07
...
cons对于数据结构来说是一个很强大的东西

(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

(define (make-point x y)
  (cons x y))

(define (x-point p)
  (car p))

(define (y-point p)
  (cdr p))

(define (make-segment start end)
  (cons start end))

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

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

(define (midpoint-segment segment)
  (cons (/ (+ (x-point (start-segment segment)) (x-point (end-segment segment))) 2)
        (/ (+ (y-point (start-segment segment)) (y-point (end-segment segment))) 2)))

(define p1 (make-point 1 2))
(define p2 (make-point 1 4))
(define s (make-segment p1 p2))

(x-point p1)
(y-point p1)
(print-point p1)
(print-point (midpoint-segment s))
相关标签: Scheme 数据结构