sicp 习题 1.38 ~ 1.41

本文探讨了使用Scheme语言实现连续分数的多种方法,包括直接递归、递归辅助过程及迭代技术,并通过具体实例展示了如何利用这些技术计算数学常数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.38
(define (cont-frac n d i k)
(if (= k 1)
(/ (n 1) (d 1))
(/ (n i) (+ (d i) (cont-frac n d (+ i 1) (- k 1))))))

(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-r (lambda (i) 1.0)
(lambda (i) 1.0)
11)

(define (frac n)
(if (= (remainder (- n 2) 3) 0)
(* 2 (+ (/ (- n 2) 3) 1))
1))


(cont-frac-r (lambda (i) 1.0)
(lambda (i)
(if (= (remainder (- i 2) 3) 0)
(* 2 (+ (/ (- i 2) 3) 1))
1))
1000)


1.39
(require (lib "math.ss"))

(define (tan-cf x k)
(define (d i)
(- (* 2 i) 1))
(define (n i)
(if (= i 1)
x
(- (* x x))))
(define (frac i)
(if (= i (+ k 1))
0
(/ (n i) (+ (d i) (frac (+ i 1))))))
(frac 1))

(tan-cf (/ pi 4) 10)

(tan-cf (- (/ pi 4)) 10)

(tan-cf (/ pi 8) 10)


1.40
(define (square x)
(* x x))

(define (cube x)
(* x x x))

(define tolerance 0.00001)

(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
;;(newline)
;;(display next)
(if (close-enough? guess next)
next
(try next))
))
(try first-guess))

(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))

(define dx 0.00001)

(define (newton-transform g)
(lambda (x)
(- x (/ (g x) ((deriv g) x)))))

(define (newton-method g guess)
(fixed-point (newton-transform g) guess))

(define (cubic-deriv a b c)
(lambda (x)
(+ (* 3 (square x)) (* 2 a x) b)))

(define (cubic a b c)
(lambda (x)
(+ (* (cube x)) (* a (square x)) (* b x) c)))

(newton-method (cubic 1 2 3) 1)
;;((cubic 1 1 1) 1)


1.41
(define (double proc)
(lambda (x)
(proc (proc x))))

(define (inc x)
(+ x 1))

((double inc) 1)

(((double double) inc) 1)

(((double (double double)) inc) 1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值