Posts

Showing posts from August, 2017

SICP Exercise 1.37

Image
Exercise 1.37.    a. An infinite  continued fraction  is an expression of the form As an example, one can show that the infinite continued fraction expansion with the  N i  and the  D i  all equal to 1 produces 1/ , where   is the golden ratio (described in section  1.2.2 ). One way to approximate an infinite continued fraction is to truncate the expansion after a given number of terms. Such a truncation -- a so-called  k -term finite continued fraction  -- has the form Suppose that  n  and  d  are procedures of one argument (the term index  i ) that return the  N i  and  D i  of the terms of the continued fraction. Define a procedure  cont-frac  such that evaluating  (cont-frac n d k)  computes the value of the  k -term finite continued fraction. Check your procedure by approximating 1/  using (cont-frac (lambda (i) 1.0)            (lambda (i) 1.0)            k) for successive values of  k . How large must you make  k  in order to get an approximation that is accurate to

SICP Exercise 1.36

Image
Exercise 1.36.   Modify  fixed-point  so that it prints the sequence of approximations it generates, using the  newline  and  display  primitives shown in exercise  1.22 . Then find a solution to  x x  = 1000 by finding a fixed point of  x     log (1000)/ log ( x ). (Use Scheme's  primitive  log  procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. (Note that you cannot start  fixed-point  with a guess of 1, as this would cause division by  log (1) = 0.) SOLUTION The code and tests are here .

SICP Exercise 1.35 golden ratio fixed point

Image
Exercise 1.35.    Show that the golden ratio     (section  1.2.2 ) is a fixed point of the transformation   x     1 + 1/ x , and use this fact to compute     by means of the   fixed-point   procedure. SOLUTION The code and tests are here .

SICP Exercise 1.34

Exercise 1.34.   Suppose we define the procedure (define (f g)   (g 2)) Then we have (f square) 4 (f (lambda (z) (* z (+ z 1)))) 6 What happens if we (perversely) ask the interpreter to evaluate the combination  (f f) ? Explain. SOLUTION The code and tests are here . Explanation: (f f) evaluates to (f 2) which in turn results in (2 2) where the first term  is expected to be a procedure. So the evaluator doesn't accept it and gives an error.