learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-6.scm (1218B)


      1 #!
      2 
      3 *Exercise 1.6:* Alyssa P. Hacker doesn't see why `if' needs to be
      4 provided as a special form.  "Why can't I just define it as an
      5      ordinary procedure in terms of `cond'?" she asks.  Alyssa's friend
      6 Eva Lu Ator claims this can indeed be done, and she defines a new
      7 version of `if':
      8 
      9 (define (new-if predicate then-clause else-clause)
     10   (cond (predicate then-clause)
     11         (else else-clause)))
     12 
     13 Eva demonstrates the program for Alyssa:
     14 
     15 (new-if (= 2 3) 0 5)
     16 5
     17 
     18 (new-if (= 1 1) 0 5)
     19 0
     20 
     21 Delighted, Alyssa uses `new-if' to rewrite the square-root program:
     22 
     23 (define (sqrt-iter guess x)
     24   (new-if (good-enough? guess x)
     25           guess
     26           (sqrt-iter (improve guess x)
     27                      x)))
     28 
     29 What happens when Alyssa attempts to use this to compute square
     30 roots?  Explain.
     31 
     32 (sqrt-iter 1 2)
     33 (new-if (good-enough? 1 2)
     34         guess
     35         (sqrt-iter (improve 1 2)
     36                    2))
     37 (new-if #f
     38         guess
     39         (new-if (good-enough? (improve 1 2) 2)
     40                 (improve 1 2)
     41                 (sqrt-iter (improve (improve 1 2) 2)
     42                            2)))
     43 and so on.
     44 
     45 new-if would first have to evaluate all of its arguments, and only then would it choose the right value using cond.
     46 
     47 !#