Exercise: Modularity, Objects, and State

1 Assignment and Local State 1.1

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”

1.2 Monte Carlo Method

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 , which returns a nonnegative number less than its input.(MIT Scheme provides such a procedure. If random is given an exact integer it returns an exact integer, but if it is given a decimal value it returns a decimal value.)

∗To treat lists, symbols and strings as parameters of a procedure, we need to put the single quote symbol (’) only at the beginning of them.

1

2 Concurrency 2.1

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

(+ low (random range))))

Which of the five possibilities in the parallel execution shown above remain if we instead serialize

execution as follows:

(define x 10)

(define s (make-serializer)) (parallel-execute

(lambda () (set! x ((s (lambda () (* x x)))))) (s (lambda () (set! x (+ x 1)))))

2.2

Give all possible values of x that can result from executing

(define x 10)

(parallel-execute (lambda () (set! x (* x x)))

(lambda () (set! x (* x x x))))

Which of these possibilities remain if we instead use serialized procedures:†

(define x 10)

(define s (make-serializer))

(parallel-execute (s (lambda () (set! x (* x x))))

(s (lambda () (set! x (* x x x)))))

†make-serializer and parallel-execute are not built-in procedures of MIT-scheme. To utilize these two pro- cedures, you have to call (require (planet dyoo/sicp-concurrency:1:2/sicp-concurrency)) in your .rkt file or put (load “PATH/parallel.scm”) at the head of the file if you are using MIT-scheme.

2

**程序代写代做C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB**

本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！

**E-mail:** [email protected] **微信:**itcsdx

如果您使用手机请先保存二维码，微信识别。如果用电脑，直接掏出手机果断扫描。