;;; This file contains two categories of figures.  The first category
;;; is that of all the programs in the book that use the character
;;; string syntax.  The second category is all others that are not
;;; in sources.ss. starting at Chapter 5.  

;;; Modifciations for later printings are noted with %%%%%%

;;; ****************************************************************

;;; Beginning of figures for Chapter 5

;;; Anonymous figure : page 140

3
n
+(3, n)
add1(+(3, n))
(add1)(+(3, n))

;;; Anonymous figure : page 141

#(app
  #(varref add1)
  (#(app
     #(varref +)
     (#(lit 3) #(varref n)))))

;;; Anonymous figure : page 144

> (read-eval-print)
--> 5
5
--> (add1 2)
3
--> (* (add1 2) (- 6 4))
6

;;; Anonymous figure : page 145

> (define parse character-string-parser)
> (run "5")
5
> (run "add1(2)")
3

> (read-eval-print)
--> 5;
5
--> add1(2);
3
--> *(add1(2),-(6,4));
6

;;; Exercise 5.1.3 : page 145

--> minus(+(minus(5), 9));
-4

;;; Exercise 5.1.4 : page 145

--> list(1, 2, 3);
(1 2 3)
--> car(cons(4, emptylist));
4

;;; Anonymous figure : page 146

--> if 1 then 2 else 3;
2
--> if -(3, +(1, 2)) then 2 else 3;
3

;;; Exercise 5.2.2 : page 147

--> equal(3, 3);
1
--> zero(sub1(5));
0
--> if greater(2, 3) then 5 else 6;
6

;;; Anonymous figure : page 148

let x = 5;
    y = 6
in +(x, y)

let x = 3
in let x = *(x, x)
   in +(x, x)

;;; 

let x = 1
in let x = +(x, 2)
   in add1(x)

;;; Anonymous figure : page 151

let f = proc (y, z) *(y, -(z, 5))
in f(2, 8)

(proc (y, z) *(y, -(z, 5)))(2, 8)

;;; Anonymous figure : page 132

let x = 5
in let f = proc (y, z) *(y, -(z, x));
       x = 28
   in f(2, 8)

;;; Exercise 5.5.4 : page 158

--> let x = 3
    in begin 
         x := add1(x);
         x := +(x, x);
         +(x, 2)
       end;
10

#(let (#(decl x #(lit 3)))
   #(begin
      #(varassign x #(app #(varref add1) (#(varref x))))
      #(begin
         #(varassign x #(app #(varref +) (#(varref x) #(varref x))))
         #(app #(varref +) (#(varref x) #(lit 2))))))

;;; Exercise 5.5.5 : page 159, 160

--> define x = 3;
--> +(x, 1);
4
--> define x = 5;
--> x;
5
--> begin 
      x := 6;
      x
    end;
6
--> let x = 3
    in begin
         define y = 4;
         +(x, y)
       end;

;;; Exercise 5.5.6 : page 160

--> let x = 3
    in x := 4;
*unspecified*

;;; Anonymous figure : page 164, 165

letrecproc
  fact(x) = if zero(x) then 1 else *(x, fact(sub1(x)))
in fact(6)

letrecproc
  even(x) = if zero(x) then 1 else odd(sub1(x));
  odd(x)  = if zero(x) then 0 else even(sub1(x))
in odd(13)

;;; Anonymous figure : page 169

let a = 3
in let p = proc (x) +(x, a);
       a = 5
   in *(a, p(2))

;;; Figure 5.7.3 : page 172

let a = 3
in let p = proc () +(x, a);
       f = proc (x, y) *(p(), y);
       a = 5
   in *(a, f(let a = 2 in a, 1))

;;; Exercise 5.7.6 : page 173

let x = 1;
    f = proc (x) +(x, y);
    g = proc (y) f(+(z, x));
    z = 2
in g(+(x, z))

;;; Anonymous figure : page 174

let x = 4
in let p = proc (y) +(x, y)
   in +(x := 7 during p(1),
        p(2))

;;; Exercise 5.7.8 : page 175

--> define x = 1;
--> define y = 2;
--> define p = proc (x, a) +(a, *(x, y));
--> letdynamic
      x = +(x, y);
      y = +(x, 3)
    in p(x, y);
16
--> p(x, y);
4

;;; Anonymous figure : page 176

standardoutput := port
during p(1, 2)

let standardoutput = port
in p(1, 2)

(proc (standardoutput) p(1, 2))(port)

;;; End of figures for Chapter 5

;;; ****************************************************************

;;; Beginning of figures for Chapter 6

;;; Anonymous figure : page 180

> (let ((p (lambda (b) (set-car! b 3)))
        (a (cons 1 2)))
    (p a)
    (car a))
3

;;; Anonymous figure : page 181

--> define p = proc (b) b[0] := 3;
--> letarray a[2]
    in begin
         a[0] := 1; a[1] := 2; 
         p(a);
         a[0]
       end;
3

;;; Figure 6.1.5 : page 185

letarray u[3]; v[2]
in begin
     u[0] := 5; u[1] := 6; u[2] := 4; v[0] := 3; v[1] := 8;
     let p = proc (x)
               begin
                 x[1] := 7;
                 x := v;
                 x[1] := 9
               end
     in p(u)
   end

;;; Anonymous figure : page 186

let p = proc (x) x := 5
in let a = 3; 
       b = 4
   in begin
        p(a);
        p(b);
        +(a, b)
      end

;;; Anonymous figure : page 187

--> define swap =
      proc (x, y)
        let temp = 0 
        in begin temp := x; x := y; y := temp end;
--> define c = 3; 
--> let b = 4 
    in begin
         swap(c, b);
         b
       end;
3 
--> c;
4 

--> define b = 2; 
--> letarray a[3]
    in begin
         a[1] := 5; 
         swap(a[1], b);
         a[1]
       end; 
2 
--> b;
5

;;; Anonymous figure : page 188, 189

--> define c = 3; 
--> define p = proc (x) x := 5; 
--> begin 
      p(add1(c));
      c
    end;
3

--> let b = 3;
        p = proc (x, y)
              begin
                x := 4;
                y
              end 
    in p(b, b);
4 

--> define swap2 =
      proc (x, y)
        begin
          x := +(x, y); y := -(x, y); x := -(x, y)
        end;

--> define b = 1;
--> define c = 2;
--> swap2(b, c);
--> b;
2
--> c;
1
--> swap2(b, b);
--> b;
0

;;; Figure 6.2.3 : page 193

letarray u[3]; v[2]
in begin
     u[0] := 5; u[1] := 6; u[2] := 4; v[0] := 3; v[1] := 8;
     let p = proc (x, y)
               begin
                 write(y);
                 x[1] := 7;
                 write(y);
                 x := v;
                 x[1] := 9;
                 write(y)
               end
     in begin 
          p(u, u[1]); 
          write(u[1])
        end

;;; Anonymous figure : page 201

--> define p = proc (x)
                 begin
                   i := 1;
                   x := 2
                 end;
--> define i = 0;
--> letarray a[2]
    in begin
         a[0] := 1;
         p(a[i]);
         writeln(a[0], a[1])
       end;
1 2

;;; Exercise 6.5.1 : page 205

letarray a[2]
in local i = 0
   in begin
        a[0] := 1; a[1] := 0;
        swap(i, a[i]); 
        writeln(a[0], a[1])
      end

;;; Exercise 6.5.2 : page 206

let p = proc (x)
          begin
            x := 3; x := +(x, 5); x := +(x, 5)
          end
in local a = 10
   in begin p(a); a end

local a = 10
in let p = proc (x)
             begin
               a := 3; a := +(x, 5); a := +(x, 5)
             end
   in begin p(+(a, a)); a end

;;; Exercise 6.5.2 : page 206 %%%%%% (a better pair of programs)

local p = proc (x)
            local a = *(x, x)
            in begin
                 x := 5; write(a)
               end
in p(3)

local a = 10
in local p = proc (x)
               begin
                 a := 3; a := +(x, 5); a := +(x, 5)
               end
   in begin p(+(a, a)); write(a) end

;;; Exercise 6.5.4 : page 206, 207

--> define x = 0;
--> int(*(x, 2), x, 1, 5, quotient(1, 2));
22.0
--> x;
5.0

;;; Exercise 6.5.5 : page 208 

local a = 10
in let p = proc (x) +(x, x)
   in p(+(begin write(a); a end, a))

;;; Exercise 6.5.5 : page 208 %%%%%% (a better exercise)

local a = 10;
      p = proc (x) +(x, x)
in p(+(begin write(a); a end, a))

;;; Exercise 6.6.1 : page 210
	   
--> (proc (x, :a = 1) +(x, a))(100);
101
--> let y = 3
    in (proc (x, :a = 1, :b = +(2, y))
          +(x, +(a, b)))
       (100, 10);
115

;;; Anonymous figure : page 211

make-window(:height = 1, :width = +(2, 3))

make-window(:width = +(2, 3), :height = 1)

;;; Exercise 6.6.3 : page 211

--> (proc (x, y) -(x, y)) (:y = 3, :x = 2);
-1
--> (proc (x, :a = 1, :b = +(2, 3))
       +(x, +(a, b)))
    (100, :b = 2);
103
;;; End of figures for Chapter 6

;;; ****************************************************************

;;; Beginning of figures for Chapter 7

;;; Anonymous figure : page 215, 216

> (define s1 (make-stack))
> (define s2 (make-stack))
> ((s1 'push!) 13)
> ((s2 'push!) 14)
> ((s2 'push!) 15)
> ((s1 'local-pushed))
1
> ((s2 'local-pushed))
2
> ((s1 'pushed))
3
> ((s2 'pushed))
3

;;; Figure 7.1.2 : page 217

--> define stackclass = 
     simpleclass (pushed) (stk, localpushed)
      (initialize = method ()
                      begin
                        &localpushed := 0;
                        &stk := emptylist
                      end;
       empty = method () null(&stk);
       push = method (x)
                begin
                  &&pushed := +(&&pushed, 1);
                  &localpushed := +(&localpushed, 1);
                  &stk := cons(x, &stk)
                end;
       pop = method ()
               if null(&stk)
               then error()
               else begin 
                      &&pushed := -(&&pushed, 1);
                      &localpushed := -(&localpushed, 1);
                      &stk := cdr(&stk)
                    end;
       top = method ()
               if null(&stk)
               then error()
               else car(&stk);
       pushed = method () &&pushed;
       localpushed = method () &localpushed)
      &&pushed := 0;
--> define stack = simpleinstance stackclass;
--> $push(stack, 7);
--> $top(stack);
7

;;; Figure 7.2.2 : page 226

define boundedstackclass = 
 class stackclass, () (bound)
  (initialize = method () 
                  begin 
                    &bound := 10; 
                    $initialize(super)
                  end;
   push = method (x) 
            if less(&localpushed, &bound)
            then $push(super, x)
            else error();
   setbound = method (x) &bound := x)
  &&pushed := 0

;;; Figure 7.2.3 : page 227

define countclass = 
 class baseobject, (classinstcount) ()
  (initialize = method () &&classinstcount := +(&&classinstcount, 1);
   howmany = method () &&classinstcount)
  &&classinstcount := 0

;;; Figure 7.2.5 : page 230

define aclass = class baseobject, (cv) (iv) () 0;

let x = 3; cv = 4
in let bclass = class aclass, () ()
                 (initialize = method () +(x, &&cv); 
                  getiv = method () &iv)
                 0
   in ...

;;; Figure 7.2.6 : page 230

define aclass = 
 class baseobject, (cv) (iv)
  (initialize = method () &iv := 8;
   getcv = method () &&cv;
   getiv = method () &iv)
  &&cv := 6;

define bclass = 
 class aclass, (cv) (iv)
  (initialize = method () 
                  begin 
                    $initialize(super);
                    &iv := 9
                  end)
  &&cv := 7;

define x = simpleinstance bclass;
define c = $getcv(x);
define i = $getiv(x)

;;; Figure 7.3.2 : page 236

define metametacountclass = 
  instance baseobject, baseobject, (classcount)
    (initialize = method () &classcount := 0);

define metacountclass = 
  instance metametacountclass, baseobject, (instcount)
    (initialize = method () begin 
                              &instcount := 0; 
                              &&classcount := +(&&classcount, 1)
                            end;
     new = method () instance self, baseobject, () ();
     howmany = method () &&classcount);

define countclass =
  instance metacountclass, baseobject, ()
    (initialize = method () &&instcount := +(&&instcount, 1);
     howmany = method () &&instcount);

define metastackclass =
  instance classof(metacountclass), metacountclass, (pushed)
    (initialize = method () begin &pushed := 0; $initialize(super) end);

define stackclass = 
  instance metastackclass, countclass, (stk, localpushed)
    (initialize = method () begin
                              &localpushed := 0;
                              &stk := emptylist;
                              $initialize(super)
                            end;
     ...);

define boundedstackclass = 
  instance classof(stackclass), stackclass, (bound) 
    (initialize = method () begin
                              &bound := 10; 
                              $initialize(super)
                            end;
    ...);

define stackinstance = $new(stackclass);

define boundedstackinstance = $new(boundedstackclass);

;;; Exercise 7.3.1 : page 238

define stackinstance2 = $new(stackclass);
define boundedstackinstance2 = $new(boundedstackclass);
define boundedstackinstance3 = $new(boundedstackclass);
define boundedstackinstance4 = $new(boundedstackclass);
$push(boundedstackinstance, 13);
$push(boundedstackinstance2, 14);
$push(boundedstackinstance2, 15)

;;; End of figures for Chapter 7

;;; ****************************************************************

;;; Beginning of figures for Chapter 8

;;; Exercise 8.7.1 : page 288

(letrec ((p1 (lambda (n) 
               (list (p3 n))))
         (p2 (lambda (n) 
               (- n 1)))
         (p3 (lambda (n) 
               (if (p4 n) 1 (* n (p5 n)))))
         (p4 (lambda (n) 
               (zero? n)))
         (p5 (lambda (n) 
               (p3 (p2 n)))))
  (p1 5))

;;; End of figures for Chapter 8

;;; ****************************************************************

;;; Beginning of figures for Chapter 9

;;; Anonymous figure : page 305

+(1, *(abort(2), +(3, 5)))

;;; Anonymous figure : page 307, 308, 309

define listindex = 
  proc (a, alst)
    letrecproc
      loop(alst) =
        if null(alst)
        then minus1
        else if equal(a, car(alst))
             then 0
             else add1(loop(cdr(alst)))
      in loop(alst)

define listindex = 
  proc (a, alst)
    letcont f
    in letrecproc
         loop(alst) =
           if null(alst)
           then f(minus1)
           else if equal(a, car(alst))
                then 0
                else add1(loop(cdr(alst)))
         in loop(alst)

define listindex = 
  proc (a, alst)
    callcc(proc (f)
             letrecproc
               loop(alst) =
                 if null(alst)
                 then f(minus1)
                 else if equal(a, car(alst))
                      then 0
                      else add1(loop(cdr(alst)))
             in loop(alst))

;;; Anonymous figure : page 312, 313

--> define toplevel = ignored;
--> letcont cont
    in toplevel := cont;

--> define resume = proc (x) error();
--> define break =
      proc (x)
        letcont cont
        in begin
             resume := cont;
             write(breakmsg);
             toplevel(x)
           end;

--> +(break(1), break(2));
break: 
1
--> resume(5);
break: 
2
--> resume(3);
8

;;; Exercise 9.4.1 : page 313, 314

--> +(10000, break(2));
break:
2
--> *(100, break(4));
break:
4
--> resume(10);
1000
--> resume(20);
2000

;;; Figure 9.4.2 : page 316

define example =
  proc ()
    letcont returncont
    in let co1 = ignored;
           co2 = ignored
       in begin
            co1 := makecoroutine(proc (initval1)
                                   begin
                                     write(1);
                                     write(initval1);
                                     initval1 := resume(co2, add1(initval1));
                                     write(1);
                                     write(initval1);
                                     initval1 := resume(co2, add1(initval1));
                                     returncont(initval1)
                                   end);
            co2 := makecoroutine(proc (initval2)
                                   begin
                                     write(2);
                                     write(initval2);
                                     initval2 := resume(co1, add1(initval2));
                                     write(2);
                                     write(initval2);
                                     resume(co1, add1(initval2))
                                   end);
            co1(33)
        end

;;; Figure 9.4.3 : page 317

define makesfcoroutine =
  proc (driver, tree)
    makecoroutine(proc (initvalue)
	            letrecproc
	              traverse(tree) =
                        if pair(tree)
                        then begin
		               traverse(car(tree));
		               if pair(cdr(tree))
                               then traverse(cdr(tree))
                               else noop
                             end
                        else resume(driver, tree)
                    in begin
	                 traverse(tree);
	                 resume(driver, 0)
                       end);

define samefringe =
  proc (tree1, tree2)
    letcont returncont
    in let co1 = ignored;
           co2 = ignored;
           driver = ignored
       in begin
            driver :=
	      makecoroutine(proc (initvalue)
		              letrecproc
		                loop() =
			          let leaf1 = resume(co1, ignored);
				      leaf2 = resume(co2, ignored)
			          in if equal(leaf1, leaf2)
				     then if zero(leaf1)
				          then returncont(1)
				          else loop()
				     else returncont(0)
			      in loop());
	    co1 := makesfcoroutine(driver, tree1);
	    co2 := makesfcoroutine(driver, tree2);
            driver(ignored)
	  end

;;; Figure 9.4.4 : page 319

define makecoroutine =
  proc (body)
    let lcs = ignored
    in letrecproc
	 newcoroutine(value) = lcs(value);
	 localresume(cont, value) =
           let value = letcont localcont
                       in begin
			    lcs := localcont;
			    cont(value)
                          end
           in begin
	        resume := localresume;
		value
              end
       in letcont exit
          in begin
	       body(localresume(exit, newcoroutine));
	       error()
             end

;;; Figure 9.5.1 : page 322

define makecoroutine =
  proc (body)
    let lcs = ignored
    in let newcoroutine = proc (value) lcs(value);
           localresume = proc (cont, value)
			   letcont localcont
	                   in begin
			        lcs := localcont;
				cont(value)
                              end
       in letcont exit
	  in wind resume := localresume
	     within begin
                      body(resume(exit, newcoroutine));
                      error()
                    end
	     unwind noop

;;; Exercise 9.5.2 : page 329

(dynamic-wind 
  (lambda () (set! resume localresume))
  (lambda () (begin
               (body (resume exit newcoroutine))
               (error)))
  (lambda () noop))

;;; Exercise 9.5.5 : page 330

letcont return
in wind write(0)
   within wind write(1)
          within wind write(2)
                 within cons(return(3), 4)
                 unwind write(200)
          unwind write(300)
   unwind write(400)

;;; Figure 9.6.1 : page 331

let x = 3;
    a = 12
in let ans = letcont out
             in let ret = letcont k
                          in x := +(a, 20) during begin
                                                    x := +(x, x);
                                                    letcont k1 in k(k1);
                                                    out(x)
                                                  end
                in begin
                     x := 50;
                     a := 100;
                     ret(5)
                   end
   in +(ans, x)

let x = 3;
    a = 12
in let ans = letcont out
             in let ret = letcont k
                          in let fluidx = +(a, 20);
                                 lexicalx = ignored
                             in wind begin
                                       lexicalx := x;
                                       x := fluidx
                                     end
                                within begin
                                         x := +(x, x);
                                         letcont k1 in k(k1);
                                         out(x)
                                       end
                                unwind begin
                                         fluidx := x;
                                         x := lexicalx 
                                       end
                in begin
                     x := 50;
                     a := 100;
                     ret(5)
                   end
   in +(ans, x)

;;; End of figures for Chapter 9

;;; ****************************************************************

;;; Beginning of figures for Chapter 10

;;; Anonymous figure : page 366

let x = 89
in let f = proc (g) g(72)
   in f(proc (y) +(x, y))

(proc (x) 
  (proc (f) f(proc (y) +(x, y)))
  (proc (g) g(72)))
(89)

;;; Figure 10.4.11

0-0: *final-v 
3-1: *pv (89) E-1
3-1: *aa #(proc (x) ..) E-1
6-4: *fv (89) E-1
6-4: *ra 89 dummy 
==> #(proc (x) ..) E-1 A6 D0
8-7: S-1 (x) 
10-9: *rv D0 
13-11: *pv (#(proc (g) ..)) E8 
13-11: *aa #(proc (f) ..) E8 
16-14: *fv (#(proc (g) ..)) E8 
16-14: *ra #(proc (g) ..) E8 
==> #(proc (f) ..) E8 A16 D10   
18-17: S8 (f) 
20-19: *rv D10 
23-21: *pv (#(proc (y) ..)) E18 
23-21: *aa #(proc (g) ..) E8 
26-24: *fv (#(proc (y) ..)) E18 
26-24: *ra #(proc (y) ..) E18 
==> #(proc (g) ..) E8 A26 D20    
28-27: S8 (g) 
30-29: *rv D20 
33-31: *pv (72) E28 
33-31: *aa #(proc (y) ..) E18
36-34: *fv (72) E28 
36-34: *ra 72 dummy 
==> #(proc (y) ..) E18 A36 D30
38-37: S18 (y) 
40-39: *rv D30 
43-41: *pv (x y) E38 
43-41: *aa #(prim-2 -) dummy 
46-44: *fv (x y) E38 
46-44: *ra 89 dummy 
49-47: *fv (y) E38 
49-47: *ra 72 dummy 
==> #(prim-2 +) dummy A49 D40
40-31: *rv D30 S18 (y) *ra 72 dummy *aa #(proc (y) ..) E18 
30-21: *rv D20 S8 (g) *ra #(proc (y) ..) E18 *aa #(proc (g) ..) E8 
20-11: *rv D10 S8 (f) *ra #(proc (g) ..) E8 *aa #(proc (f) ..) E8 
10-1: *rv D0 S-1 (x) *ra 89 dummy *aa #(proc (x) ..) E-1
0-0: *final-v 
161

;;; Exercise 10.4.5 : page 371

letrecproc p(f, g, n) = 
  if zero(n)
     then f(0)
     else let t = if even(n)
                  then proc (x) f(+(x, n))
                  else proc (x) g(+(x, n))
          in p(t, g, -(n, 1))                    
in p(proc (x) x, proc (x) x, 4)

;;; End of figures for Chapter 10

;;; ****************************************************************

;;; Beginning of figures for Chapter 11

;;; Anonymous figure : page 383, 385

> (define char-seq-head car)
> (define char-seq-tail cdr)
> (define keywords-list '())
> (define raw-data
    (append (string->list "abc is 652") (list #\^)))  ;;; #\nul ==> #\^
> (set! x (scan-once scanner-start-state raw-data))
> x
#(scanner-answer
 #(token variable abc)
 (#\space #\i #\s #\space #\6 #\5 #\2 #\^))
> (set! x (scan-once scanner-start-state 
              (scanner-answer->unscanned x)))
> x
#(scanner-answer
 #(token variable is)
 (#\space #\6 #\5 #\2 #\^))
> (set! x (scan-once scanner-start-state 
              (scanner-answer->unscanned x)))
> x
#(scanner-answer
 #(token number 652)
 (#\^))
> (set! x (scan-once scanner-start-state 
              (scanner-answer->unscanned x)))
> x
#(scanner-answer
 #(token end-marker *)
 (#\^))

;;; Anonymous figure : page 386

> (scan-char-seq scanner-start-state
    (append (string->list "abc is 652") (list #\^)))
(#(token variable abc)
 #(token variable is)
 #(token number 652)
 #(token end-marker *))

;;; Exercise 11.2.4 : page 386

"!? % ( \\
)\"(
% )"

;;; Anonymous figure : page 391

begin x := foo; while x do x := (x + bar) end

#(compound-command
  #(assignment-command x #(var-expression foo))
  #(while-command
    #(var-expression x)
    #(assignment-command x
      #(sum-expression #(var-expression x) #(var-expression bar)))))

;;; Exercise 11.3.3 : page 398

> (read-eval-print)
--> x:=y
#(assignment-command x #(var-expression y))
--> begin x:=y ; u := (v + u) end
#(compound-command
 #(assignment-command x #(var-expression y))
  #(assignment-command u
   #(sum-expression #(var-expression v) #(var-expression u))))
--> begin x:=y ; 
          uu := ((x + v) + u) end
#(compound-command
 #(assignment-command x #(var-expression y))
 #(assignment-command uu
  #(sum-expression
   #(sum-expression #(var-expression x) #(var-expression v))
   #(var-expression u))))
--> end
>

;;; Exercise 11.3.5 : page 402

letrecproc
  add(x, y) = if x
              then y
              else add(sub1(x), add1(y));
  mult(x, y) = if x
               then 0
               else if sub1(x)
                    then y
                    else add(y, mult(sub1(x), y))
in let x = 1;
       y = 2;
       z = proc (x, y) mult(add(3, x), y);
       w = proc () add := proc (x, y) x
   in letarray a[8];
               b[add(x, mult(y, x))]
      in begin
           b[add(x, 1)] := 17;
           y := mult(x, b[y]);
           y := add(x, y);
           w();
           z(x, y)
         end

;;; Figure 11.3.5 : 403

#(letrecproc
   (#(procdecl add (x y)
       #(if #(varref x)
	    #(varref y)
	    #(app #(varref add)
		  (#(app #(varref sub1) (#(varref x)))
		   #(app #(varref add1) (#(varref y)))))))
    #(procdecl mult (x y)
       #(if #(varref x)
	    #(lit 0)
	    #(if #(app #(varref sub1) (#(varref x)))
		 #(varref y)
		 #(app #(varref add)
		       (#(varref y)
			#(app #(varref mult)
			      (#(app #(varref sub1) (#(varref x)))
			       #(varref y)))))))))    
   #(let (#(decl x #(lit 1))
          #(decl y #(lit 2))
          #(decl z #(proc (x y)
		      #(app #(varref mult)
			    (#(app #(varref add) (#(lit 3) #(varref x)))
			     #(varref y)))))
          #(decl w #(proc () #(varassign add #(proc (x y) #(varref x))))))
      #(letarray (#(decl a #(lit 8))
	          #(decl b #(app #(varref add)
			         (#(varref x)
			          #(app #(varref mult) (#(varref y) #(varref x)))))))
	 #(begin
	    #(arrayassign #(varref b)
                          #(app #(varref add) (#(varref x) #(lit 1))) #(lit 17))
            #(begin
               #(varassign y
		  #(app #(varref mult) 
                        (#(varref x) #(arrayref #(varref b) #(varref y)))))
               #(begin
                  #(varassign y #(app #(varref add) (#(varref x) #(varref y))))
                  #(begin
                     #(app #(varref w) ())
                     #(app #(varref z) (#(varref x) #(varref y))))))))))

;;; Exercise 11.3.6 : page 402, 404

--> let p = proc (@x, y) 
              begin 
                y := +(y, 1);
                x := +(x, 1) 
              end;
        z = 5
    in begin 
         p(z, z);
         z
       end;
6

;;; Anonymous figure : page 413

(#(check/drop begin)
 #(process-nt command)
 #(check/drop semicolon)
 #(process-nt command)
 #(check/drop end)
 #(reduce compound))

;;; End of figures for Chapter 11

;;; ****************************************************************

;;; Beginning of figures for Chapter 12

;;; Anonymous figure : page 422

> (run "if sum(1, five) then 3 else six")
3

;;; Anonymous figure : page 429

#(varref-instruc six env-reg
 #(cont r0 
  #(halt-instruc r0)))

;;; Anonymous figure : page 430
> (compile "sum(five, 9)")
#(varref-instruc five env-reg
 #(cont r2
  #(lit-instruc 9
   #(cont r4
    #(emptyargs-instruc
     #(cont r5
      #(rest-instruc r4 r5
       #(cont r3
        #(rest-instruc r2 r3
         #(cont r1
          #(sum-instruc r1
           #(cont r0
            #(halt-instruc r0)))))))))))))

(varref-action 'five init-env
 (lambda (r2)
  (lit-action 9
   (lambda (r4)
    (emptyargs-action
     (lambda (r5)
      (rest-action r4 r5
       (lambda (r3)
        (rest-action r2 r3
         (lambda (r1)
          (sum-action r1
           (lambda (r0)
            (halt-action r0)))))))))))))

;;; Anonymous figure : page 431

> (compile "if 0 then sum(3, 4) else sum(five, six)")
#(lit-instruc 0
 #(cont r1
  #(test-instruc r1
   #(lit-instruc 3
    #(cont r3
     #(lit-instruc 4
      #(cont r5
       #(emptyargs-instruc
        #(cont r6
         #(rest-instruc r5 r6
          #(cont r4
           #(rest-instruc r3 r4
            #(cont r2
             #(sum-instruc r2
              #(cont r0
               #(halt-instruc r0)))))))))))))
   #(varref-instruc five env-reg
    #(cont r8
     #(varref-instruc six env-reg
      #(cont r10
       #(emptyargs-instruc
        #(cont r11
         #(rest-instruc r10 r11
          #(cont r9
           #(rest-instruc r8 r9
            #(cont r7
             #(sum-instruc r7
              #(cont r0
               #(halt-instruc r0))))))))))))))))

;;; Exercise 12.1.2 : page 433

> (code->scheme (compile "sum(five, 9)"))
(varref-action 'five init-env 
  (lambda (r2) ...))
>   (eval (code->scheme (compile "sum(five, 9)")))
14

;;; Anonymous figure : page 435

> (compile "sum(five, 9)")
#(varref-instruc five env-reg
 #(cont r0
  #(lit-instruc 9
   #(cont r0
    #(emptyargs-instruc
     #(cont r3
      #(rest-instruc r0 r3
       #(cont r2
        #(rest-instruc r0 r2
         #(cont r1
          #(sum-instruc r1
           #(cont r0
            #(halt-instruc r0)))))))))))))

;;; Anonymous figure : page 438

#(varref-instruc five env-reg
 #(cont r0 ()
  #(lit-instruc 9
   #(cont r1 (r0)
    #(emptyargs-instruc
     #(cont r2 (r1 r0)
      #(rest-instruc r1 r2
       #(cont r1 (r0)
        #(rest-instruc r0 r1
         #(cont r0 ()
          #(sum-instruc r0
           #(cont r0 ()
            #(halt-instruc r0)))))))))))))

;;; Anonymous figure : page 442

> (compile "sum(five, 9)")
(#(fetch five env-reg r0)
 #(move-imm 9 r1)
 #(cons-1 r2)
 #(cons-3 r1 r2 r1)
 #(cons-3 r0 r1 r0)
 #(sum-list r0 r0)
 #(stop r0))

;;; Exercise 12.3.2 : page 446

"sum(f(x), if z then x else y)"

;;; Anonymous figure : page 451. 452

> (compile "(proc (x) x)(17)")
#(close-instruc
 #(extend-env-instruc (x) args-reg env-reg
  #(cont env-reg ()
   #(varref-instruc x env-reg
    #(cont r0 ()
     #(return-instruc r0 k-reg)))))
 env-reg
 #(cont r0 ()
  #(lit-instruc 17
   #(cont r1 (r0)
    #(emptyargs-instruc
     #(cont r2 (r1 r0)
      #(rest-instruc r1 r2
       #(cont r1 (r0)
	#(save-instruc (save-regs k-reg env-reg)
	 #(cont ignore-reg ()
	  #(apply-closure-instruc r0 r1
	   #(cont r0 ()
	    #(restore-instruc
	     #(cont ignore-reg ()
	      #(halt-instruc r0)))))))))))))))

(close-action
 (lambda (env-reg args-reg k-reg)
  (extend-env-action '(x) args-reg env-reg
   (lambda (env-reg)
    (varref-action 'x env-reg
     (lambda (r0)
      (return-action r0 k-reg))))))
 init-env
 (lambda (r0)
  (lit-action 17
   (lambda (r1)
    (emptyargs-action
     (lambda (r2)
      (rest-action r1 r2
       (lambda (r1)
        (save-action '(save-regs k-reg env-reg)
         (lambda ()
          (apply-closure-action r0 r1
           (lambda (r0)
            (restore-action
             (lambda ()
              (halt-action r0)))))))))))))))

(define save-action
  (lambda (lst goto)
    (goto)))

(define restore-action
  (lambda (goto)
    (goto)))

;;; End of figures for Chapter 12

