sicp-ex-3.14



<< Previous exercise (3.13) | Index | Next exercise (3.15) >>


meteorgan

  
  
  The procedure mystery will produce the inverse order of x.  
 v 
 => (a b c d) 
 w 
 => (d c b a) 

Rather Iffy

 Call : (mystery '(a b c d)) :
 +------------------------------------------------------------------------+
 |     x:    .                                                            |
 |     y: /  |                                                            |
 +------------------------------------------------------------------------+
             |                                           +----------------+
             |                                           | temp:  .       |
             |                                           +------- |-------+
             |                                                    |
             |                                                    |
             --------               ------------------------------
                     v              v
                 +---+---+      +---+---+      +---+---+      +---+----
                 | . | .------->| . | .------->| . | .------->| . | / |
                 +-|-+---+      +---+---+      +-|-+---+      +-|-+---+
                   v              v              v              v
                 +---+          +---+          +---+          +---+
                 | a |          | b |          | c |          | d |
                 +---+          +---+          +---+          +---+
 Result after 1 cycle through loop
 +------------------------------------------------------------------------+
 |     x:    .----------------------                                      |
 |     y:    .                      |                                     |
 +----------------------------------|-------------------------------------+
             |                      |                    +----------------+
             |                      |                    | temp:  .       |
             |                      |                    +--------|-------+
             |                      |                             |
             |                      |                             |
             --------               |              |---------------
                     v              v              v
                 +---+---+      +---+---+      +---+---+      +---+----
                 | . | /        | . | .------->| . | .------->| . | / |
                 +-|-+---+      +---+---+      +-|-+---+      +-|-+---+
                   v              v              v              v
                 +---+          +---+          +---+          +---+
                 | a |          | b |          | c |          | d |
                 +---+          +---+          +---+          +---+
 
 Result after 2 cycle
 +------------------------------------------------------------------------+
 |     x:    .-------------------------------------                       |
 |     y:    .                                     |                      |
 +-------------------------------------------------|----------------------+
             |                                     |     +----------------+
             |                                     |     | temp:  .       |
             |                                     |     +--------|-------+
             |                                     |              |
             |                                     |              |
             -----------------------|              |              |
                                    |              |              |
                                    |              |              |
                     ----------------------        |              |
                     v              v      |       v              v
                 +---+---+      +---+---+  |   +---+---+      +---+----
                 | . | /        | . | .----    | . | .------->| . | / |
                 +-|-+---+      +---+---+      +-|-+---+      +-|-+---+
                   v              v              v              v
                 +---+          +---+          +---+          +---+
                 | a |          | b |          | c |          | d |
                 +---+          +---+          +---+          +---+
 Result after 3 cycle
 +------------------------------------------------------------------------+
 |     x:    .-----------------------------------------                   |
 |     y:    .                                         |                  |
 +-----------------------------------------------------|------------------+
             |                                         | +----------------+
             |                                         | | temp:  /       |
             |                                         | +----------------+
             |                                         |
             |                                         |
             --------------------------------------    -----------
                                                   |              |
                                    ----- -------------- --       |
                                    |              |      |       |
                     ----------------------        |      |       |
                     v              v      |       v      |       v
                 +---+---+      +---+---+  |   +---+---+  |   +---+----
                 | . | /        | . | .----    | . | .----    | . | / |
                 +-|-+---+      +---+---+      +-|-+---+      +-|-+---+
                   v              v              v              v
                 +---+          +---+          +---+          +---+
                 | a |          | b |          | c |          | d |
                 +---+          +---+          +---+          +---+
 Result after 4 cycle
 +------------------------------------------------------------------------+
 |     x:    /                                                            |
 |     y:    .                                                            |
 +------------------------------------------------------------------------+
             |                                           +----------------+
             |                                           | temp:  /       |
             |                                           +----------------+
             |
             |
             -----------------------------------------------------|
                                                    ---------------------
                                                   |              |      |
                                                   |              |      |
                                    ----- -------------- --       |      |
                                    |              |      |       |      |
                     ----------------------        |      |       |      |
                     v              v      |       v      |       v      |
                 +---+---+      +---+---+  |   +---+---+  |   +---+---+  |
                 | . | /        | . | .----    | . | .----    | . | .----|
                 +-|-+---+      +---+---+      +-|-+---+      +-|-+---+
                   v              v              v              v
                 +---+          +---+          +---+          +---+
                 | a |          | b |          | c |          | d |
                 +---+          +---+          +---+          +---+
 Result after 5 cycle
 Printed value  :
 w : (d c b a)