`

sicp 2.1小节习题解答

 
阅读更多
    昨天晚上读了2.1节,今天开始做下习题,这节相当有趣,数据抽象的概念解释的很清晰。
习题2.1,make-rat能正确地处理正负数,加几个判断条件即可:
<!---->(define (make-rate n d)
  (let ((g (gcd n d)))
    (cond ((or (and (negative? n) (positive? d)) (and (positive? n) (positive? d))) (cons (/ n g) (/ d g)))
          (else
           (cons (opposition (/ n g)) (opposition (/ d g)))))))

习题2.2,首先定义make-point,x-point,y-point三个过程:
<!---->(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))

线段是由两点组成,在此基础上定义make-segment,start-segment,end-segment过程:
<!---->(define (make-segment s e)
  (cons s e))
(define (start-segment segment)
  (car segment))
(define (end-segment segment)
  (cdr segment))

OK,然后定义题目要求的midpoint-segment求线段中间点坐标:
<!---->(define (midpoint-segment segment)
  (let ((start (start-segment segment))
        (end (end-segment segment)))
    (make-point (/ (+ (x-point start) (x-point end)) 2) (/ (+ (y-point start) (y-point end)) 2))))

测试一下:
<!---->> (define start (make-point 10 10))
> (define end (make-point 0 0))
> (define segment (make-segment start end))
> (print-point (midpoint-segment segment))

(5,5)

 习题2.3,这题目主要是考察对过程抽象的理解,对于计算一个矩形的周长和面积来说,需要两个信息:长度和宽度。因此,首先假设我们已经有3个过程:make-rectang用于创建矩形,width用于返回宽,length用于返回长。那么周长和面积可以写为:
<!---->(define (perimeter rectang)
  (* 2 (+ (width rectang) (length rectang))))
(define (area rectang)
  (* (width rectang) (length rectang)))
具体如何实现make-rectang,width,length3个过程与周长、面积的计算实现了抽象隔离,具体实现的改变不需要修改 perimeter、area两个过程。矩形可以表示为一条有向线段和距离,有向线段是矩形的一条边,与它平行的另一条边的距离是d。因此定义下这三个过 程:
<!---->(define (make-rectang segment d)
    (cons segmeng d))
(define (length rectang)
   (car rectang))
(define (width rectang)
  (let ((seg (car x)))
    (let ((s (start-segment seg))
          (e (end-segment seg)))
      (sqrt (+ (square (- (x-point s)
                          (x-point e)))
               (square (- (y-point s)
                          (y-point e))))))))

square过程就是平方过程,假设已经定义。



dennis 2007-05-23 11:57 发表评论
分享到:
评论

相关推荐

    SICP习题解答,主要第一章的内容习题答案

    《SICP习题解答,主要第一章的内容习题答案》 SICP,全称《Structure and Interpretation of Computer Programs》(计算机程序的构造和解释),是计算机科学领域的一本经典教材,由MIT(麻省理工学院)的 Harold ...

    SICP 习题答案

    《计算机程序的构造和解释》...通过解答SICP的习题,读者将深入理解这些概念,并能运用到实际的编程实践中。习题旨在促进对这些基本原理的深入思考,帮助程序员建立坚实的基础,进而在面对复杂的编程挑战时能游刃有余。

    sicp第二章练习题的解答

    《SICP(Structure and Interpretation of Computer Programs)》是一本经典的计算机科学教材,由Harold Abelson和Gerald Jay Sussman所著,它强调了程序设计的基础和原理,特别是函数式编程思想。第二章主要探讨了...

    SICP中文第二版

    SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版

    sicp in python 中文 sicp 中文

    sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download&gt;&gt;&gt;https://github.com/wizardforcel/sicp-py-zh

    SICP 解题集

    《SICP解题集》是一份专注于探讨和解答《结构与解释程序》(Structure and Interpretation of Computer Programs,简称SICP)一书中习题的资源。SICP是计算机科学领域的一本经典教材,由Harold Abelson、Gerald Jay ...

    北京大学,计算机程序构造和解释(SICP)课件,裘宗燕老师主讲

    《计算机程序构造和解释》(SICP,Structure and Interpretation of Computer Programs)是一本具有深远影响力的计算机...压缩包中的“SICP 北大课件”文件可能包含课件、讲义、习题解答等资料,是学习SICP的宝贵资源。

    SICP(python中文带书签)

    《计算机程序的构造与解释》(Structure and Interpretation of Computer Programs,简称SICP)是一本备受推崇的经典计算机科学教材,由Harold Abelson和Gerald Jay Sussman撰写,并由MIT出版社出版。这本书以其深入...

    sicp 2.2.4节图形语言

    《SICP 2.2.4 节:图形语言》是计算机科学经典教材《结构与解释程序》(Structure and Interpretation of Computer Programs)中的一个重要章节,它深入介绍了如何利用编程来创建图形,以及如何设计和理解复杂的计算...

    SICP-Python版本

    SICP-Python版本

    SICP 使用的scheme解释器

    SICP 使用的scheme解释器 以前叫DrScheme

    Python SICP epub版本

    Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用

    sicp-Structure and Interpretation of Computer Programs

    ### SICP——《计算机程序的结构与解释》 #### 一、概述 《计算机程序的结构与解释》(Structure and Interpretation of Computer Programs, 简称SICP)是一本由MIT电气工程与计算机科学系教授Harold Abelson和...

    SICP LISP AI

    《SICP》全称是《Structure and Interpretation of Computer Programs》,中文译为《计算机程序的构造和解释》。这是一本经典的计算机科学教材,由Harvard大学的 Harold Abelson 和 Gerald Jay Sussman 教授撰写,...

    a_book_sicp_py

    本书名为《a_book_sicp_py》,是一本以Python语言为基础介绍设计模式和计算机科学基础的书籍。根据描述和部分内容,可以提炼出以下知识点: 1. 编程语言的重要性:在计算机科学的宽泛领域中,编程语言扮演着至关...

    PyPI 官网下载 | sicp-0.0.1b102.dev4.tar.gz

    标题中的"PyPI 官网下载 | sicp-0.0.1b102.dev4.tar.gz"指的是从Python的官方包索引(Python Package Index,简称PyPI)上下载的一个名为"sicp"的软件包的版本号为0.0.1b102.dev4的压缩文件,其格式是tar.gz。...

    sicp in python 中文版 sicp

    sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 download : https://github.com/wizardforcel/sicp-py-zh

    sicp 2nd 英文chm

    《Structure and Interpretation of Computer Programs》(简称SICP)是计算机科学领域的一部经典教材,由Harold Abelson和Gerald Jay Sussman撰写,第二版(2nd Edition)通常被称为SICP 2nd。这本书是麻省理工学院...

    sicp 2016 from

    ### 结构与解释计算机程序 (SICP) #### 标题和描述中的核心知识点解析 **《结构与解释计算机程序》(Structure and Interpretation of Computer Programs, SICP)** 是由哈佛大学的 Harold Abelson 和麻省理工学院...

Global site tag (gtag.js) - Google Analytics