#lang sicp
;;https://www.bilibili.com/video/BV1Xx41117tr
;;https://github.com/DeathKing/Learning-SICP/tree/master/SrtCN
;;第四节
;;;现在到了数学抽象中最关键的一步:让我们忘记这些符号所表示的对象
;;;......(数学家)不应在这里停步,有许多操作可以应用与这些符号,
;;;而根本不必考虑它们到底代表着什么东西。
;;;---hermann wey1,思维的数学方式
;;;中心思想为要建立层次化的系统
;;;建立抽象屏障将细节隔离在底层
;;;将细节与你所工作的高层环境隔离开
;;;the key idea is that you're going to build the system in layers
;;;and set up abstraction barries that isolate the details at the lower layers
;;;from the thing that's going on at the upper layers
(make-rat n d)
;(numer xxx) --> n
;(denom xxx) ---> d
(define (+rat x y)
(make-rat
(+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (*rat x y)
(make-rat
(* (numer x) (numer y))
(* (denom x) (denom y))))
;;构造函数 constructors
;;选择函数 selectors
;;list structure
;;pairs
;;(cons x y)
;;constructs a pair whose first part is x and whose second part is y
;;(car p)
;;selects the first part of the pair p
;;(cdr p)
;;selects the second part of the pair p
(cons 2 3)
;;box and pointer notation
(define (make-rat n d)
(cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define a (make-rat 1 2))
(define b (make-rat 1 4))
(define ans (+rat a b))
(numer ans) ---> 6
(denom ans) ---> 8
;;;构造的时候,化到最简
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g)
(/ d g))))
;;; data abstraction
;;; use | +rat *rat -rat
;;; abstraction layer | make-rat numer denom
;;; representation | pairs
;;;defining +rat without data abstractio
;;;也就是没有抽象层(构造函数和选择函数)
;;;在这样的系统中,并没有一个有理数的概念
(define (+rat x y)
(cons (+ (* (car x) (cdr y))
(* (car y) (cdr x)))
(* (cdr x) (cdr y))))
;;;将数据的使用和表示分离
;;;编程中最重要的原理:naming
;;;有理数的概念naming和实体implementing(或者叫实现)分离
;;;同一个概念有不同的实现
;;;数据结构的概念和实现
;;;另一种表示有理数的方法
(define (make-rat n d)
(cons n d))
;;提取的时候,化到最简
(define (numer x)
(let ((g (gcd (car x) (cdr x))))
(/ (car x) g)))
(define (denom x)
(let ((g (gcd (car x) (cdr x))))
(/ (cdr x) g)))
;;;第一种方式,构造的时候,化到最简
;;;第二种方式,提取的时候,化到最简
;;************只关注use****************
;;only give a name,然后继续编程
;;representing vectors in the plane
(define (make-vector x y) (cons x y))
(define (xcor p) (car p))
(define (ycor p) (cdr p))
;;用数字来表示点
;;用点来构造向量
;;用向量来表示线段
;;representing line segments
(define (make-seg p q) (cons p q))
(define (seg-start s) (car s))
(define (seg-end s) (cdr s))
(define (midpoint s)
(let ((a (seg-start s))
(b (seg-end s)))
(make-vector
(average (xcor a) (xcor b))
(aberage (ycor a) (ycor b)))))
(define (length s)
(let
((dx (- (xcor (seg-end s))
(xcor (seg-start s))))
(dy (- (ycor (seg-end s))
(ycor (seg-start s)))))
(sqrt (+ (square dx)
(square dy)))))
;;我们构建的是一种层次系统
;;what we built is a layered system
;;; segments
;;;
;;;---------------------------
;;;make-seg seg-start seg-end
;;;----------------------------
;;;
;;; vectors
;;;
;;;----------------------------
;;;make-vector xcor ycor
;;;----------------------------
;;;
;;; pairs
;;;
;;;cons就是把什么东西组合起来
;;abstract data
;;fulfill this contract for any choice of n and d
;; if x = (make-rat n d)
;; then (/ (numer x) (denom x)) = (/ n d)
;;有理数就是满足这些公理(contrat)的过程(这里是三个过程,构造函数和两个选择函数)
;;axiom for pairs
;;for any x and y
;;(car (cons x y)) is x
;;(cdr (cons x y)) is y
;;pure abstraction
;;you don't see any data in here, what you see is a lambda
(define (cons a b)
(lambda (pick)
(cond ((= pick 1) a)
((= pick 2) b))))
(define (car x) (x 1))
(define (cdr x) (x 2))
;;建立数据抽象不需要数据
;;we don't need data at all to build these data abstractions
;;我们可以用过程来完成所有的事情
;;we can do everything in terms of procedures
;;过程是一种对象
;;过程是概念实体,是对象
;;procedures are conceptual entities, objects
sicp-mit-4
最新推荐文章于 2023-07-26 17:41:09 发布