練習2.2
(define (make-point x y)練習2.3(cons x y))
(define (x-point p)
(car p))
(define (y-point p)
(cdr p))
(define (make-segment m n)
(cons m n))
(define (start-segment s)
(car s))
(define (end-segment s)
(cdr s))
(define (midpoint-segment s)
(make-point (/ (+ (x-point (start-segment s))
(x-point (end-segment s)))
2.0)
(/ (+ (y-point (start-segment s))
(y-point (end-segment s)))
2.0)))
1 ]=> (print-point (midpoint-segment
(make-segment
(make-point 1 3)
(make-point 3 5))))
(2,4)
;; 平面矩形的建構函式和選擇函式;; 建構函式, 使用兩條線段構造平面矩形
(make-rectangle segment1 segment2)
;; 返回平面矩形相鄰兩邊中左邊的線段
(left-segment-rectangle rectangle)
;; 返回平面矩形相鄰兩邊中右邊的線段
(right-segment-rectangle rectangle)
;; 根據建構函式和選擇函式實現計算平面矩形周長的過程
;; 首先實現計算線段長度的過程, 這裡借用2.2中的關於線段和點的過程
(define (segment-length segment)
(point-length (start-segment segment) (end-segment segment)))
(define (point-length start end)
(sqrt (+ (square (- (x-point start) (x-point end)))
(square (- (y-point start) (y-point end))))))
;; 然後實現計算平面矩形周長的過程
(define (perimeter-rectangle rectangle)
(+ (* 2 (segment-length (left-segment-rectangle rectangle)))
(* 2 (segment-length (right-segment-rectangle rectangle)))))
;; 對建構函式和選擇函式做兩種實現
;; 平面矩形可以由相對(不在同一條直線上)的兩個長度相同的平行線段構成, 這裡沒有對是否平行校驗
(define (make-rectangle segment1 segment2)
(cons segment1 segment2))
(define (left-segment-rectangle rectangle)
(car rectangle))
(define (right-segment-rectangle rectangle)
(make-segment (start-segment (car rectangle))
(start-segment (cdr rectangle))))
;; 驗證
(perimeter-rectangle
(make-rectangle
(make-segment (make-point 0 0)
(make-point 3 0))
(make-segment (make-point 3 4)
(make-point 6 4))))
;value: 16
;; 平面矩形可以由相交(起點相同且不在同一條直線上)的的兩條線段構成, 這裡沒有對十分在同一條直線校驗
(define (make-rectangle segment1 segment2)
(cons segment1 segment2))
(define (left-segment-rectangle rectangle)
(car rectangle))
(define (right-segment-rectangle rectangle)
(cdr rectangle))
;; 驗證
(perimeter-rectangle
(make-rectangle
(make-segment (make-point 0 0)
(make-point 3 0))
(make-segment (make-point 0 0)
(make-point 3 4))))
;value: 16
;; 對於平面矩形的面積
;; 首先實現點到直線距離的過程
;; 點(x0, y0)到直線ax+by+c=0的距離公式為|ax0+by0+c|/√a^2+b^2
;; 將直線方程變換為y=kx+b形式,得到距離公式為|kx0-y0+b|/√k^2+1
(define (point-to-segment point segment)
(let ((fun (get-k-b segment)))
(/ (abs (+ (* (car fun) (car point))
(- (cdr point))
(cdr fun)))
(sqrt (+ (square (car fun)) 1)))))
;; k = (y1-y2)/(x1-x2)
;; b = y1-x1*k
(define (get-k-b segment)
(let ((k (/ (- (cdr (start-segment segment))
(cdr (end-segment segment)))
(- (car (start-segment segment))
(car (end-segment segment))))))
(cons k (- (cdr (start-segment segment))
(* k (car (start-segment segment)))))))
;; 由此實現計算平面矩形面積的過程
(define (area-rectangle rectangle)
(* (segment-length (left-segment-rectangle rectangle))
(point-to-segment (end-segment (right-segment-rectangle rectangle))
(left-segment-rectangle rectangle))))
;; 由平行線段構造平面矩形驗證計算面積的過程
(define (right-segment-rectangle rectangle)
(make-segment (start-segment (car rectangle))
(start-segment (cdr rectangle))))
(area-rectangle (make-rectangle
(make-segment (make-point 0 0)
(make-point 3 0))
(make-segment (make-point 3 4)
(make-point 6 4))))
;value: 12
;; 由相交線段構造平面矩形驗證計算面積的過程
(define (right-segment-rectangle rectangle)
(cdr rectangle))
(area-rectangle (make-rectangle
(make-segment (make-point 0 0)
(make-point 3 0))
(make-segment (make-point 3 0)
(make-point 6 4))))
;value: 12
;; 這裡將平面矩形理解成了平行四邊形,-_-!,不過嘗試
(area-rectangle (make-rectangle
(make-segment (make-point 0 0)
(make-point 3 0))
(make-segment (make-point 3 0)
(make-point 3 4))))
;; 也可以得到正確結果
SICP學習筆記(1 1 4 1 1 5)
sicp學習筆記 1.1.4 1.1.5 周銀輝 書接上一回,這裡是我在學習1.1.4 1.1.5時的一些筆記 1,標準過程與復合過程 以 scheme為例,作為一門語言,其內建了一些原始過程 或稱之為標準的過程 與scheme環境中,比如,實際上與某個內建過程關聯 繫結 了起來,該過程接收兩個引數...
SICP學習筆記(1 1 1 1 1 3)
sicp學習筆記 1.1.1 1.1.3 周銀輝 sicp,即structure and interpretation of computer programs,電腦程式的構造和解釋,是mit 麻省理工學院 一門經典課程,相信很多程式愛好者都讀過,最近我也抽空讀了讀,挺有意思的。算是對大學知識 高數...
SICP學習筆記(一)
一直在斷斷續續地看sicp,確實字字珠璣,今天總算看了前兩章,習題做了一部分,不過日後肯定還是要回頭重看的,現在略微回頭總結一下。1 好的程式語言應提供的三種機制 基本表示式 組合 抽象 實際上我們自己也可以為特定問題制定自己的 語言 是否具有上述能力是這個語言是否強大的關鍵 2 過程作為黑箱的抽象...