【SICP练习】144 练习3.82
来源:互联网 发布:windows 运行spark 编辑:程序博客网 时间:2024/06/01 10:29
练习3-82
原文
Exercise 3.82. Redo exercise 3.5 on Monte Carlo integration in terms of streams. The stream version of estimate-integral will not have an argument telling how many trials to perform. Instead, it will produce a stream of estimates based on successively more trials.
代码
(define (random-in-range low high) (let ((range (- high low))) (+ low (* (random) range)))) (define (random-number-pairs low1 high1 low2 high2) (cons-stream (cons (random-in-range low1 high1) (random-in-range low2 high2)) (random-number-pairs low1 high1 low2 high2))) (define (monte-carlo experiment-stream passed failed) (define (next passed failed) (cons-stream (/ passed (+ passed failed)) (monte-carlo (stream-cdr experiment-stream) passed failed))) (if (stream-car experiment-stream) (next (+ passed 1) failed) (next passed (+ failed 1)))) (define (estimate-integral p x1 x2 y1 y2) (let ((area (* (- x2 x1) (- y2 y1))) (randoms (random-number-pairs x1 x2 y1 y2))) (scale-stream (monte-carlo (stream-map p randoms) 0 0) area))) (define (sum-of-square x y) (+ (* x x) (* y y))) (define f (lambda (x) (not (> (sum-of-square (- (car x) 1) (- (cdr x) 1)) 1)))) (define pi-stream (estimate-integral f 0 2 0 2))
感谢您的访问,希望对您有所帮助。 欢迎大家关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp
5 0
- 【SICP练习】144 练习3.82
- 【SICP练习】2 练习1.6
- 【SICP练习】3 练习1.7
- 【SICP练习】4 练习1.8
- 【SICP练习】5 练习1.9
- 【SICP练习】6 练习1.10
- 【SICP练习】7 练习1.11
- 【SICP练习】8 练习1.12
- 【SICP练习】9 练习1.15
- 【SICP练习】10 练习1.16
- 【SICP练习】11 练习1.17
- 【SICP练习】12 练习1.18
- 【SICP练习】13 练习1.19
- 【SICP练习】14 练习1.20
- 【SICP练习】15 练习1.21
- 【SICP练习】16 练习1.22
- 【SICP练习】17 练习1.23
- 【SICP练习】18 练习1.24
- 快速部署安装libart_lgp
- dialogprogress,进度条
- Sql Server2008——远程过程调用失败
- 第三十一次codeforces竞技结束 #297 Div 2
- mac svn 服务配置 客户端连接说明
- 【SICP练习】144 练习3.82
- Linux学习之CentOS(七)--CentOS下j2ee环境搭建
- 第四周 项目四 指向学生类的指针
- matlab IO
- Mysql错误消息 语言设置
- #leetcode#Valid Sudoku
- hdu1541 Stars 树状数组水题
- 输出一个数字各个位数和
- 常见HTTP状态(304,503)