`
zhangle
  • 浏览: 26605 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

sicp 习题 2.6 ~ 2.10

    博客分类:
  • SICP
F# 
阅读更多
2.6
(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))

((zero 4) 3)
(add-1 5 1)

2.7
(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))

(define (div-interval x y)
  (mul-interval x 
                (make-interval (/ 1.0 (upper-bound y))
                               (/ 1.0 (lower-bound y)))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval 1.2 2.2))

(define new-interval (add-interval one-interval two-interval))

(lower-bound new-interval)
(upper-bound new-interval)

2.8
(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
  (add-interval x (make-interval (- 0 (lower-bound y)) (- 0 (upper-bound y)))))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))

(define (div-interval x y)
  (mul-interval x 
                (make-interval (/ 1.0 (upper-bound y))
                               (/ 1.0 (lower-bound y)))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval 1.2 2.2))

(define new-add-interval (add-interval interval-1 interval-2))

(define new-mul-interval (mul-interval interval-1 interval-2))

(define new-div-interval (div-interval interval-1 interval-2))

(define new-sub-interval (sub-interval interval-1 interval-2))

(lower-bound new-sub-interval)

(upper-bound new-sub-interval)

2.9
(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
  (add-interval x (make-interval (- 0 (lower-bound y)) (- 0 (upper-bound y)))))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))

(define (div-interval x y)
  (mul-interval x 
                (make-interval (/ 1.0 (upper-bound y))
                               (/ 1.0 (lower-bound y)))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define (interval-width interval)
  (/ (- (upper-bound interval) (lower-bound interval)) 2))

(define (oper-interval-width v1 v2)
  (let ((w1 (interval-width v1))
        (w2 (interval-width v2)))
    (lambda(x) (x w1 w2))))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval 1.2 2.2))

(interval-width (add-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) +)

(interval-width (sub-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) -)

(interval-width (mul-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) *)

(interval-width (div-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) /)

2.10
(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
  (add-interval x (make-interval (- 0 (lower-bound y)) (- 0 (upper-bound y)))))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))

(define (div-interval x y)
  (let ((product-y (* (lower-bound y) (upper-bound y))))
    (if (< product-y 0)
        (display "illegal number")
        (mul-interval x 
                      (make-interval (/ 1.0 (upper-bound y)) (/ 1.0 (lower-bound y)))))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define (interval-width interval)
  (/ (- (upper-bound interval) (lower-bound interval)) 2))

(define (oper-interval-width v1 v2)
  (let ((w1 (interval-width v1))
        (w2 (interval-width v2)))
    (lambda(x) (x w1 w2))))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval -1.2 2.2))

(div-interval interval-1 interval-2)
分享到:
评论

相关推荐

    SICP 习题答案

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

    sicp第二章练习题的解答

    以上就是基于文件名推测的SICP第二章练习题相关知识点。这些内容深入地涵盖了函数式编程的基础和应用,对于提升编程思维和技能大有裨益。实际的学习过程中,通过阅读和理解这些代码,结合原书的理论部分,将有助于...

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

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

    sicp:我的 SICP 练习

    在" sicp-master "这个压缩包中,可能包含的是对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中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版

    SICP 解题集

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

    sicp-solutions:SICP练习解决方案

    "sicp-solutions"是一个针对该书练习题的解答集,主要使用了Scheme语言,一个Lisp方言,而具体的实现环境是mit-scheme 9.2。 Scheme语言是Lisp家族的一员,以其简洁的语法和强大的函数式编程特性闻名。在"sicp-...

    SICP(python中文带书签)

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

    ChezScheme9.5.4.exe

    scheme编译器最新版,Windows下直接安装即可运行,sicp刷题必备。祝大家早日学到sicp之精髓~

    sicp 2.2.4节图形语言

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

    sicp-Structure and Interpretation of Computer Programs

    书中大量的练习题和示例代码鼓励读者动手实践,加深对概念的理解。此外,SICP还引入了许多先进的编程思想和技术,如函数式编程、递归、抽象数据类型等,这些都对现代软件开发产生了深远的影响。 #### 四、总结 ...

    SICP-Python版本

    SICP-Python版本

    SICP 使用的scheme解释器

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

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

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

    Python SICP epub版本

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

    SICP LISP AI

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

    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。...

    a_book_sicp_py

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

Global site tag (gtag.js) - Google Analytics