Chapter 3

[*]Ex 3.1

(a)
Strictly speaking, the definition of Algol 68 allows parentheses wherever brackets ([ and ]) are allowed. Fortunately or unfortunately, the a68toc compiler flags this as an error.
(b)
The apostrophes should be replaced by quote symbols.
(c)
The value 2.0 in the row-display cannot be coerced to a value of mode INT in a strong context (or any context, for that matter).
[*]Ex 3.2
[]INT first 4 odd numbers = (1,3,5,7)
[*]Ex 3.3

(a)
8
(b)
1
(c)
3
[*]Ex 3.4

(a)
1 LWB a, 1 UPB a, 2 LWB a, 2 UPB a, 3 LWB a,
3 UPB a
(b)
LWB b, UPB b

[*]Ex 3.5

(a)
6
(b)
(9,10,11,12)
(c)
(4,8,12,16)
[*]Ex 3.6

(a)
r[3,2]
(b)
r[2,]
(c)
r[,3]

[*]Ex 3.7
   [][]CHAR months=
      ("January","February","March",
       "April","May","June",
       "July","August","September",
       "October","November","December")
[*]Ex 3.8

(a)
30
(b)
(0.0,-5.4)
(c)
11.4
(d)
(6,7,8)
(e)
"pqrst"

[*]Ex 3.9
This exercise is self-marking, but here is a program to print the answer to the first exercise:-
   PROGRAM ex3 9 CONTEXT VOID
   USE standard
   BEGIN
     [,]INT m = ((1,2,3,4),(5,6,7,8),
              (9,10,11,12),(13,14,15,16));

     print(("m[2,2]=",m[2,2],newline,
          "m[3,]=",m[3,],newline,
          "m[,2 UPB m]=",m[,2 UPB m],
          newline))
   END
   FINISH

[*]Ex 3.10

(a)
Man bites dog
(b)
bbbii

[*]Ex 3.11
   PROGRAM ex3 11 CONTEXT VOID
   USE standard
   BEGIN
      FOR num TO 25
      DO
         print((num^3,newline))
      OD
   END
   FINISH
[*]Ex 3.12
   PROGRAM ex3 12 CONTEXT VOID
   USE standard
   BEGIN
      FOR c FROM ABS "Z" BY -1 TO ABS "A"
      DO
         print(REPR c)
      OD
   END
   FINISH

[*]Ex 3.13
The main difficulty lies in computing the letter to print. The first solution uses numbers and REPR:-
   PROGRAM ex3 13a CONTEXT VOID
   USE standard
   BEGIN
      FOR row TO 5
      DO
         FOR letter TO 4
         DO
            print((REPR((row-1)*5
                     +letter+ABS"@"),",")
         OD;

         print((REPR(row*5 + ABS "@"),
                newline))
      OD
   END
   FINISH
The second solution uses an actual alphabet and a modified inner loop. Note that the formulæ in the FROM and TO constructs are elaborated once only: before the inner loop is elaborated for the first time in each elaboration of the outer loop:-
   PROGRAM ex3 13b CONTEXT VOID
   USE standard
   BEGIN
      []CHAR alphabet =
       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[@1];

      FOR row TO 5
      DO
         INT row5 = row*5;

         FOR letter FROM row5-4 TO row5-1
         DO
            print((alphabet[letter],","))
         OD;

         print((alphabet[row5],newline))
      OD
   END
   FINISH
[*]Ex 3.14
The most difficult part is in declaring the multiple. print will quite happily take the 3-dimensional multiple as its parameter:
   PROGRAM ex3 14 CONTEXT VOID
   USE standard
   BEGIN
      [,,]REAL m=(((1e-7,1e-6),
                   (1e-5,1e-4)),
                  ((1e-3,1e-2),
                   (1e-1,1.0)));
      print(m)
   END
   FINISH

[*]Ex 3.15

(a)
The brackets for the row-display should be replaced by parentheses.
(b)
The number of integers in each row should be the same.
(c)
Nothing. The denotation of an apostrophe is not doubled.
[*]Ex 3.16

(a)
[1:2,1:3]
(b)
[1:3]
(c)
[1:2]
[*]Ex 3.17

(a)
(6,5,4) []INT
(b)
(8,5,2) []INT
(c)
(7,4) []INT
(d)
((6,5),(3,2)) [,]INT
[*]Ex 3.18
"abcabcabcdefg"
[*]Ex 3.19
Notice the means of avoiding the use of BY:
   PROGRAM ex3 18 CONTEXT VOID
   USE standard
   BEGIN
      []CHAR alphabet =
            "abcdefghijklmnopqrstuvwxyz";
      []INT by = (1,6,11,16,21,26);

      FOR c BY --5 TO UPB alphabet
      DO
         print(alphabet[c])
      OD
   END
   FINISH

Sian Mountbatten 2012-01-19