计算机程序的构造和解释 折半查找法
2016-05-22 21:58
483 查看
#lang racket (define (search f neg-point pos-point) (let ((midpoint (average neg-point pos-point))) (if (close-enough? neg-point pos-point) midpoint (let ((test-value (f midpoint))) (cond ((positive? test-value) (search f neg-point midpoint)) ((negative? test-value) (search f midpoint pos-point)) (else midpoint)))))) (define (average x y) (/ (+ x y) 2)) (define (close-enough? x y) (< (abs (- x y)) 0.000000000000001)) (define (half-interval-method f a b) (let ((a-value (f a)) (b-value (f b))) (cond ((and (negative? a-value) (positive? b-value)) (search f a b)) ((and (negative? b-value) (positive? a-value)) (search f b a)) (else (error "Values are not of opposite sign" a b))))) ;: 求pi 的值: (half-interval-method sin 2.0 4.0)
求方程的根 : x3−2x−3=0
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3)) 1.0 2.0)
相关文章推荐
- 用环境模型来理解javascript中的闭包
- 对嵌套映射的理解
- SICP Exercise 1.13
- sicp习题答案和范例代码 - 第一章
- sicp习题答案和范例代码 - 第二章 2.1
- SICP第一章——构造过程抽象之程序设计的基本元素(1.1)笔记及习题解答
- SICP-《计算机程序的构造和解释》之习题分析与收获——练习1.30
- SICP-《计算机程序的构造和解释》之习题分析与收获——练习1.29
- 游走于数据与代码之间
- 换零钱问题迭代解法
- Structure and Interpretation of Computer Programs 读书笔记
- 应用序和正则序
- [SICP Notes] 1.1 The Elements of Programming
- 《计算机程序的构造与解释》习题自编代码(第1章)(未完待更)
- 【Structure and interpretation of Computer Programs 】学习记录兼导言
- 第一章笔记整理
- SICP-练习2.34
- SICP-练习2.17
- 计算斐波纳契数,分析算法复杂度
- SICP-求幂