sicp习题试解 (2.2)

类别:编程语言 点击:0 评论:0 推荐:

; ======================================================================
;
; Structure and Interpretation of Computer Programs
; (trial answer to excercises)
;
; 计算机程序的构造和解释(习题试解)
;
; created: code17 04/18/05
; modified:
; (保持内容完整不变前提下,可以任意转载)
; ======================================================================


;; SICP No.2.2

(define (make-segment s e) (cons s e))
(define (start-segment x) (car x))
(define (end-segment x) (cdr x))

(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))

(define (midpoint-segment x)
(define (avr f a b)
(/ (+ (f a) (f b)) 2))
(let ((s (start-segment x))
(e (end-segment x)))
(make-point (avr x-point s e)
(avr y-point s e))))

;; Test-it:
;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
;; > (let ((test-seg (make-segment (make-point -10 20)
;; (make-point 80 -40))))
;; (midpoint-segment test-seg))
;; (35 . -10)
;;
;; 为了简便,没有使用原文中提供的print-point函数,
;; 而直接使用了MzScheme Interpreter返回的原始值,即一个pair

本文地址:http://com.8s8s.com/it/it22251.htm