ModulaTor logo, 7.8KB

The ModulaTor

Oberon-2 and Modula-2 Technical Publication

Ubaye's First Independent Modula-2 & Oberon-2 Journal! Nr. 83, May-2001,

Variable parameter list in ISO Modula-2

by Guenter Dotzel, ModulaWare

Two short, simple example programs illustrate the use of ISO Modula-2's array and record constructors:

  1. GenOut shows how to implement procedures with variable number of parameters; using open arrays and ISO Modula-2's array constructors. (This program was written 10 years ago, when ISO Modula-2 did not yet exist. At this time, the Modula/R constructor syntax with [...] instead of ISO Modula-2's {...} was used.)

    MODULE GenOut; (* Show variable parameter-list in Modula-2 using open arrays and array constructors written by Elmar Baumgart, ModulaWare, 29-May-1991 *) FROM InOut IMPORT WriteReal, Write, WriteLn, WriteString; TYPE String = ARRAY[0..500] OF CHAR; r1 = ARRAY[0..0] OF REAL; c1 = ARRAY[0..0] OF COMPLEX; r2 = ARRAY[0..1] OF REAL; c2 = ARRAY[0..1] OF COMPLEX; r3 = ARRAY[0..2] OF REAL; c3 = ARRAY[0..2] OF COMPLEX; s1 = ARRAY[0..0] OF String; s2 = ARRAY[0..1] OF String; s3 = ARRAY[0..2] OF String; PROCEDURE wr(x: ARRAY OF REAL); VAR i: INTEGER; BEGIN FOR i:= 0 TO HIGH(x) DO WriteReal(x[i], 0); WriteString(', '); END; WriteLn; END wr; PROCEDURE wc(x: ARRAY OF COMPLEX); VAR i: INTEGER; BEGIN FOR i:= 0 TO HIGH(x) DO Write('('); WriteReal(RE(x[i]), 0); WriteString(', '); WriteReal(IM(x[i]), 0); WriteString('), '); END; WriteLn; END wc; PROCEDURE ws(x: ARRAY OF String); VAR i: INTEGER; BEGIN FOR i:= 0 TO HIGH(x) DO WriteString(x[i]); WriteString(', '); END; WriteLn; END ws; CONST cpi = CMPLX(3.1415, 2.*3.1415); BEGIN ws (s1{ 'one real and one complex' }); wr (r1{ 3.1415 }); wc (c1{ CMPLX(3.1415, 2.*3.1415) }); ws (s2{ 'two reals', 'two complexs' }); wr (r2{ 3.1415, 3.1415 }); wc(c2{ cpi, CMPLX(2.,0.)*cpi }); wr (r3{ 3.1415, 3.1415, 3.1415 }); END GenOut.
    The output of GenOut is as follows:

    one real and one complex, 3.141500 , ( 3.141500 , 6.283000 ), two reals, two complexs, 3.141500 , 3.141500 , ( 3.141500 , 6.283000 ), ( 6.283000 , 12.56600 ), 3.141500 , 3.141500 , 3.141500 ,

  2. Magic is just another example of array and record constructors, showing the Li Shu's alphamagablitity test. A square is defined to be magic if the sum of all rows, columns and diagonales add up to the same value. For example the following 3*3 square is magic:


    That's easily verified, but are the following nine consecutive primes magic? (Are they really consecutive primes? If you don't believe, find out.)


    The following output free program checks whether some 3*3 squares are magic; if they are not magic, it terminates with a forced run-time error (halt statement); The program was written 11 years ago. I believe I found some or all numbers used below in a Scientific American article.

    MODULE Magic; (* Show array and record constructor features of MVR written by Guenter Dotzel, ModulaWare, 07-Jun-1990 *) TYPE C = RECORD x,y,z: INTEGER END; T = ARRAY [0..2] OF C; VAR r,c: C; t: T; PROCEDURE AddRowColD(p: T); TYPE CA = ARRAY [0..7] OF LONGREAL; PROCEDURE Magic (a: ARRAY OF LONGREAL): BOOLEAN; VAR i: INTEGER; BEGIN FOR i:= 1 TO VAL(INTEGER, HIGH(a)) DO IF a[0] <> a[i] THEN RETURN FALSE END; END; RETURN TRUE; END Magic; BEGIN (* a square is defined to be magic if the sum of all rows, columns and diagonales add up to the same value *) IF NOT Magic(CA{ LFLOAT(p[0].x) + LFLOAT(p[1].x) + LFLOAT(p[2].x), (* three rows *) LFLOAT(p[0].y) + LFLOAT(p[1].y) + LFLOAT(p[2].y), LFLOAT(p[0].z) + LFLOAT(p[1].z) + LFLOAT(p[2].z), LFLOAT(p[0].x) + LFLOAT(p[0].y) + LFLOAT(p[0].z), (* three columns *) LFLOAT(p[1].x) + LFLOAT(p[1].y) + LFLOAT(p[1].z), LFLOAT(p[2].x) + LFLOAT(p[2].y) + LFLOAT(p[2].z), LFLOAT(p[0].x) + LFLOAT(p[1].y) + LFLOAT(p[2].z), (* two diagonales *) LFLOAT(p[2].x) + LFLOAT(p[1].y) + LFLOAT(p[0].z)}) THEN HALT END; END AddRowColD; BEGIN (* The "lo shu" magic square: *) AddRowColD(T{ {2,7,6}, {9,5,1}, {4,3,8}}); (* Lee Sallows' "li shu" magic square: *) AddRowColD(T{ { 5,22,18}, {28,15, 2}, {12, 8,25}}); (* "li shu's" alphamagic partner square (alphamagic in respect to English: "five" has 4 letters, "Twentytwo" has 9 letters and so on) is magic too? *) AddRowColD(T{ {4, 9, 8}, {11,7, 3}, { 6,5,10}}); (* Now test "li shu's" "alphamagablitity": *) AddRowColD(T{ {LENGTH("five"), LENGTH("twentytwo"), LENGTH("eighteen")}, {LENGTH("twentyeight"), LENGTH("fifteen"), LENGTH("two")}, {LENGTH("twelve"), LENGTH("eight"), LENGTH("twentyfive")}}); (* The following is a square with 9 consecutive primes (if you don't believe that they are all prime numbers, then find out). Is it magic too? *) AddRowColD(T{ {1480028201,1480028129,1480028183}, {1480028153,1480028171,1480028189}, {1480028159,1480028213,1480028141}}); (* This output free program shows that the 3*3 squares are magic if it terminates normal successfully otherwise it terminates with an error (halt statement). *) END Magic.

The publication of the above examples does not mean that I'd like these ISO Modula-2 extensions; actually, I'm not a featurist.
IMPRESSUM: The ModulaTor is an unrefereed journal. Technical papers are to be taken as working papers and personal rather than organizational statements. Items are printed at the discretion of the Editor based upon his judgement on the interest and relevancy to the readership. Letters, announcements, and other items of professional interest are selected on the same basis. Office of publication. The Editor of The ModulaTor is Günter Dotzel; he can be reached at [email deleted due to spam]

Most of the ModulaTor back-issues are available from website navigator

[ Home | Site_index | Legal | OpenVMS_compiler | Alpha_Oberon_System | ModulaTor | Bibliography | Oberon[-2]_links | Modula-2_links | General interesting book recommendations ]
© (2001) by
Created 11-May-2001, last revised 11-May-2001