其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
sicp习题试解 (1.37)

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

; ======================================================================
;
; Structure and Interpretation of Computer Programs
; (trial answer to excercises)
;
; 计算机程序的构造和解释(习题试解)
;
; created: code17 03/06/05
; modified:
; (保持内容完整不变前提下,可以任意转载)
; ======================================================================

;; SICP No.1.37

;; 递归版本
(define (cont-frac-r n d k)
(define (frac i)
(if (= i (+ k 1))
0
(/ (n i) (+ (d i) (frac (+ i 1))))))
(frac 1))

;; 迭代版本
(define (cont-frac-i n d k)
(define (frac i v)
(if (= i 0)
v
(frac (- i 1) (/ (n i) (+ (d i) v)))))
(frac k 0))

;; 另外,cont-frac也可使用前面章节所定义的accumulate过程来定义


;; Test-it;;
;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
;; > (define cont-frac cont-frac-i)
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 1)
;; 1.0
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 2)
;; 0.5
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 3)
;; 0.6666666666666666
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 4)
;; 0.6000000000000001
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 5)
;; 0.625
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 6)
;; 0.6153846153846154
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 7)
;; 0.6190476190476191
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 8)
;; 0.6176470588235294
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 9)
;; 0.6181818181818182
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 10)
;; 0.6179775280898876
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 11)
;; 0.6180555555555556
;; > (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 12)
;; 0.6180257510729613
;; >
;;
;; 由运行结果可知,当k>=11时,可以达到小数点后4位的精度。



相关文章

相关软件