Posts

Showing posts from October, 2018

SICP Exercise 3.5 estimate-integral

Image
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 <  3 2 . 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 ma

SICP Exercise 3.4 call-the-cops

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 . SOLUTION I converted the explicit 'dispatch' procedure into a lambda function. This made it more elegant. The code is here: SICP Exercise 3.4 call-the-cops

SICP Exercise 3.3 password protected account

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" SOLUTION The code is here: SICP Exercise Exercise 3.3 password protected account

SICP Exercise 3.2 make-monitored

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 SOLUTION The code is here: SICP Exercise 3.2 make-monitored

SICP Exercise 3.1 make-accumulator

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 SOLUTION The code is here: SICP Exercise 3.1 make-accumulator

SICP Chapter 2 - Systems with Generic Operations All Exercises in one program

All Generic Operations Problems in One Program (Look at the bottom part for results from all the tests from Exercise 2.77 to Exercise 2.97)

SICP Exercise 2.97 reduce rational function

Thus, here is how to reduce a rational function to lowest terms: Compute the GCD of the numerator and denominator, using the version of  gcd-terms  from exercise  2.96 . When you obtain the GCD, multiply both numerator and denominator by the same integerizing factor before dividing through by the GCD, so that division by the GCD will not introduce any noninteger coefficients. As the factor you can use the leading coefficient of the GCD raised to the power 1 +  O 1  -  O 2 , where  O 2  is the order of the GCD and  O 1  is the maximum of the orders of the numerator and denominator. This will ensure that dividing the numerator and denominator by the GCD will not introduce any fractions. The result of this operation will be a numerator and denominator with integer coefficients. The coefficients will normally be very large because of all of the integerizing factors, so the last step is to remove the redundant factors by computing the (integer) greatest common divisor of all the coeffi