Task

Show how to concatenate two arrays in your language.



If this is as simple as array1 + array2, so be it.




BEGIN  ! Concatenate arrays - of REAL, here;
 
    CLASS REAL_ARRAY(N); INTEGER N;
    BEGIN
        REAL ARRAY DATA(1:N);
 
        ! Return a new REAL_ARRAY containing
        ! the values from this REAL_ARRAY 
        ! followed by the values from other;
        REF(REAL_ARRAY) PROCEDURE CONCAT(other);
            REF(REAL_ARRAY) other;
        BEGIN
            REF(REAL_ARRAY) C;
            INTEGER I;
 
            C :- NEW REAL_ARRAY(N + other.N);
 
            FOR I := 1 STEP 1 UNTIL N DO
                C.DATA(I) := DATA(I);
 
            FOR I := 1 STEP 1 UNTIL other.N DO
                C.DATA(N + I) := other.DATA(I);
 
            CONCAT :- C;
        END;
 
        ! Fill DATA;
        REF(REAL_ARRAY) PROCEDURE linearFill(start, stride);
            REAL start, stride;
        BEGIN
            linearFillFrom(DATA, 1, N, start, stride);
            linearFill :- this REAL_ARRAY
        END;
 
        PROCEDURE out(sink); REF(printfile) sink;
        BEGIN
            INTEGER i;
            FOR i := 1 STEP 1 UNTIL N DO
                sink.OUTFIX(DATA(i), 2, 7);
            sink.OUTIMAGE;
        END;
    END REAL_ARRAY;
 
    ! "The problem" is not array as an input parameter:
    !  I don't know how to
    ! "pass a new ARRAY out of a PROCEDURE";
    REF(REAL_ARRAY) PROCEDURE concatenate(a, b);
        REAL ARRAY a, b;
    BEGIN
        INTEGER i, a_, N, b_, M;
        REF(REAL_ARRAY) c;
        a_ := LOWERBOUND(a, 1) - 1;
        N := UPPERBOUND(a, 1) - a_;
        b_ := LOWERBOUND(a, 1) - 1;
        M := UPPERBOUND(b, 1) - b_;
        c :- NEW REAL_ARRAY(N + M);
 
        FOR i := 1 STEP 1 UNTIL N DO
            c.DATA(i) := a(a_+i);
        ! for readability, don't
        !  reduce one index expression to a variable
        FOR i := 1 STEP 1 UNTIL M DO
            c.DATA(N + i) := b(b_+i);
 
        concatenate :- c;
    END concatenate REAL ARRAYs;
 
    ! two more convenience PROCEDUREs;
    PROCEDURE linearFillFrom(a, from, inclusive, start, stride);
            REAL ARRAY a; ! passed by reference;
            INTEGER from, inclusive;
            REAL start, stride;
    BEGIN
        INTEGER i;
        FOR i := from STEP 1 UNTIL inclusive DO
            a(i) := start + stride * (i - from)
    END;
    PROCEDURE linearFill(a, start, stride);
            REAL ARRAY a;
            REAL start, stride;
        linearFillFrom(a, LOWERBOUND(a, 1), UPPERBOUND(a, 1),
                       start, stride);
 
 
    REF(REAL_ARRAY) X;
    REAL ARRAY u(1:3), v(1:4);
    linearFill(u, 3, 7);
    linearFill(v, 0, 5);
    concatenate(u, v).out(SYSOUT);
 
    X :- NEW REAL_ARRAY(3).linearFill(1, 2);
    X.out(SYSOUT);
    X.CONCAT(NEW REAL_ARRAY(4)
                .linearFill(-1, -3)).out(SYSOUT);
END.