Sketch of Solutions to Exercises

1.3.1

“2,2”: e, “2,2”

“3,2”: e, “2”, “3”, “3,2”

1.3.2

“2,2”: 2

“3,2’: none

1.3.3

(a)
  v:= ? 
  write v 
  do 
    read  x 
    if  x = v + 2  then  reject 
  until  x = v 
  do 
    if eof  then  accept 
    read  x 
    if  x = v + 2 then  reject 
  until  false 

(b)
  v1 : = ? 
  do 
    v2:= v1-2 
    write v2 
  or 
    v2 := v1 + 2 
    write v1 
  until  true 
  do 
    read  x 
    until  x =v1 
  do 
    read  x 
  until  x =v2 
  do 
    if  eof  then  accept 
    read  x 
  until  false 
 

(c)
  v:=  ? 
  write  v 
  do 
    if eof  then  accept 
    read  x 
  until  x = v 
  do 
    if eof  then  accept 
    read  x 
  until  x = v 
  do 
    read  x 
  until  x = v 
  do 
    if eof  then accept 
    read  x 
  until  false 

(d)
  count := 0 
  do 
    do 
      read  x 
      count :=  count + 1 
    until  count := x 
    do 
      write  x 
      do 
        if eof  then accept 
        read  x 
      until  false 
    or 
      x := x 
    until  true 
  until  false 

(e)
  v := ? 
  if  v = 0 then reject 
  write  v 
  count := 0 
  do 
    read x 
    if  v = x  then 
    count := count + 1 
  until  count = v 
  do 
    if eof then accept 
    read  x 
  until  x = v 

(f)
   do 
      repeat :=  ? 
     do 
       read x 
     until  repeat = x 
     do 
       read  x 
     until  repeat = x 
     do 
       if eof  then accept 
       read  x 
     until  false 
    or 
       hole := ? 
       before := 0 
       after := 0 
     do 
        if before = after = 1  then 
            if  eof  then  accept 
        read  x 
        if  x < hole then 
         before := 1 
        if  x > hole then 
         after := 1 
      until  x = hole 
   until  true 

1.3.4

(a)
   miss :=  ? 
   do 
     if eof  then accept 
     read  x 
   or 
     x :=  ? 
     write x 
   until  x = miss 

(b)
   read x 
   do 
     y :=  ? 
     write y 
   or 
     write  x 
     do 
       if eof  then accept 
       read  x 
     until  false 
   until  false 

1.3.5
  do 
     P1 
  or 
     P2 
  until  true 

1.3.6
(1,<0,0>,<>,<1,2,1>,<>)  |- (2,<1,0>,<>,<1,2,1>,<>)
 |- (3,<1,0>,<>,<1,2,1>,<1>)
 |- (4,<1,0>,<>,<1,2,1>,<1>)
 |- (5,<1,1>,<1>,<2,1>,<1>)
 |- (6,<1,1>,<1>,<2,1>,<1>)
 |- (7,<1,1>,<1>,<2,1>,<1>)