免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6486 | 回复: 17
打印 上一主题 下一主题

SICP 习题参考答案 3.1  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-05 11:12 |只看该作者 |倒序浏览
此帖为 SICP 章节 3.1 的参考答案。为保持可读性,请勿直接在本帖讨论。讨论请另开新帖。


论坛徽章:
0
2 [报告]
发表于 2008-10-05 11:17 |只看该作者

Exercise 3.1.

Exercise 3.1.  An accumulator is a procedure that is called repeatedly with a single numeric argument and accumulates its arguments into a sum. Each time it is called, it returns the currently accumulated sum. Write a procedure make-accumulator that generates accumulators, each maintaining an independent sum. The input to make-accumulator should specify the initial value of the sum; for example

(define A (make-accumulator 5))
(A 10)
15
(A 10)
25

论坛徽章:
0
3 [报告]
发表于 2008-10-05 11:18 |只看该作者

Exercise 3.1. 答案


  1. (define (make-accumulator init)
  2.   (lambda (x)
  3.     (set! init (+ init x))
  4.     init))
复制代码

论坛徽章:
0
4 [报告]
发表于 2008-10-05 11:25 |只看该作者

Exercise 3.2.

Exercise 3.2.  In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, f, that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to mf is the special symbol how-many-calls?, then mf returns the value of the counter. If the input is the special symbol reset-count, then mf resets the counter to zero. For any other input, mf returns the result of calling f on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure:

(define s (make-monitored sqrt))

(s 100)
10

(s 'how-many-calls?)
1

论坛徽章:
0
5 [报告]
发表于 2008-10-05 11:25 |只看该作者

Exercise 3.2. 答案


  1. (define (make-monitored f)
  2.   (let ((cnt 0))
  3.     (lambda (x)
  4.       (cond ((eq? x 'how-many-calls?) cnt)
  5.             (else (set! cnt (+ cnt 1))
  6.                   (f x))))))
复制代码

论坛徽章:
0
6 [报告]
发表于 2008-10-05 12:00 |只看该作者

Exercise 3.3.

Exercise 3.3.  Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in

(define acc (make-account 100 'secret-password))

The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint:

((acc 'secret-password 'withdraw) 40)
60

((acc 'some-other-password 'deposit) 50)
"Incorrect password"

论坛徽章:
0
7 [报告]
发表于 2008-10-05 12:01 |只看该作者

  1. (define (make-account balance password)
  2.   (define (withdraw amount)
  3.     (if (>= balance amount)
  4.         (begin (set! balance (- balance amount))
  5.                balance)
  6.         "Insufficient funds"))
  7.   
  8.   (define (deposit amount)
  9.     (set! balance (+ balance amount))
  10.     balance)
  11.   
  12.   (define (dispatch pass m)
  13.     (cond ((not (eq? password pass)) (lambda (x) "Incorrect password"))
  14.           ((eq? m 'withdraw) withdraw)
  15.           ((eq? m 'deposit) deposit)
  16.           (else (error "Unkonwn request -- MAKE-ACCOUNT" m))))
  17.   dispatch)
复制代码

论坛徽章:
0
8 [报告]
发表于 2008-10-05 12:42 |只看该作者

Exercise 3.4.

Exercise 3.4.  Modify the make-account procedure of exercise 3.3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops.

论坛徽章:
0
9 [报告]
发表于 2008-10-05 12:43 |只看该作者

Exerciese 3.4. 答案


  1. (define (make-account balance password)
  2.   (let ((cnt 0))
  3.     (define (call-the-cap)
  4.       (lambda (x) "Cap Called"))
  5.    
  6.     (define (withdraw amount)
  7.       (if (>= balance amount)
  8.           (begin (set! balance (- balance amount))
  9.                  balance)
  10.           "Insufficient funds"))
  11.    
  12.     (define (deposit amount)
  13.       (set! balance (+ balance amount))
  14.       balance)
  15.    
  16.     (define (dispatch pass m)
  17.       (cond ((= cnt 7) (lambda (x) "Account Locked"))
  18.             ((not (eq? password pass)) (begin
  19.                                          (set! cnt (+ cnt 1))
  20.                                          (if (= cnt 7) (call-the-cap)
  21.                                              (lambda (x) "Incorrect password"))))
  22.             (else (set! cnt 0)
  23.                   (cond ((eq? m 'withdraw) withdraw)
  24.                         ((eq? m 'deposit) deposit)
  25.                         (else (error "Unkonwn request -- MAKE-ACCOUNT" m))))))
  26.     dispatch))
复制代码

[ 本帖最后由 win_hate 于 2008-12-6 22:00 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-12-06 22:31 |只看该作者

Exercise 3.5.

Exercise 3.5.  Monte Carlo integration is a method of estimating definite integrals by means of Monte Carlo simulation. Consider computing the area of a region of space described by a predicate P(x, y) that is true for points (x, y) in the region and false for points not in the region. For example, the region contained within a circle of radius 3 centered at (5, 7) is described by the predicate that tests whether (x - 5)^2 + (y - 7)^2< 32. To estimate the area of the region described by such a predicate, begin by choosing a rectangle that contains the region. For example, a rectangle with diagonally opposite corners at (2, 4) and (8, 10) contains the circle above. The desired integral is the area of that portion of the rectangle that lies in the region. We can estimate the integral by picking, at random, points (x,y) that lie in the rectangle, and testing P(x, y) for each point to determine whether the point lies in the region. If we try this with many points, then the fraction of points that fall in the region should give an estimate of the proportion of the rectangle that lies in the region. Hence, multiplying this fraction by the area of the entire rectangle should produce an estimate of the integral.

Implement Monte Carlo integration as a procedure estimate-integral that takes as arguments a predicate P, upper and lower bounds x1, x2, y1, and y2 for the rectangle, and the number of trials to perform in order to produce the estimate. Your procedure should use the same monte-carlo procedure that was used above to estimate . Use your estimate-integral to produce an estimate of by measuring the area of a unit circle.

You will find it useful to have a procedure that returns a number chosen at random from a given range. The following random-in-range procedure implements this in terms of the random procedure used in section 1.2.6, which returns a nonnegative number less than its input.8

(define (random-in-range low high)
  (let ((range (- high low)))
    (+ low (random range))))

[ 本帖最后由 win_hate 于 2008-12-6 22:33 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP