[Oberon] Eliminating RETURN statements
Hans Klaver
hklaver at dds.nl
Wed Apr 8 18:09:12 CEST 2020
Hi Jörg,
Thanks for your prompt reply.
> Here my proposals to make you code examples simpler:
>
> (* Oberon-07 *)
> PROCEDURE F (x, y: INTEGER): INTEGER;
> IF x < y THEN x := y – x
> ELSE x := x – y
> END
> RETURN x
> END F;
>
> In this special case, this is perhaps a little clearer (
> PROCEDURE F (x, y: INTEGER): INTEGER; RETURN ABS(x-y) END F;
Brilliant!
Inline ABS(x - y) is even clearer (and faster).
It illustrates that the example chosen by prof. Wirth can hardly be called 'real world' ;-)
> Your code
> (* Oberon-07, right output *)
> PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
> VAR k: INTEGER; OK: BOOLEAN;
> BEGIN
> k := 0; OK := TRUE;
> WHILE (k <= piecemax[i]) & OK DO
> IF p[i][k] THEN
> IF puzzl[j + k] THEN OK := FALSE END
> END;
> INC(k)
> END
> RETURN OK
> END Fit;
>
> My proposal:
> PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
> VAR k: INTEGER; OK: BOOELAN;
> BEGIN
> k := 0;
> WHILE (k <= piecemax[i]) DO
> OK := ~( p[i, k] & puzzl[j + k] ); INC(k)
> END
> RETURN OK END Fit;
At first sight to me it looked like a correct rewrite.
Unfortunately this code leads to an "array index out of bounds" runtime exception (Trap 1 in System Oberon) of the puzzl array.
> Your code:
> (* Oberon-07 *)
> (...)
> k := j; res := 0; exit := FALSE;
> WHILE (k <= size) & (~ exit) DO
> IF ~ puzzl[k] THEN res := k; exit := TRUE
> ELSE INC(k)
> END
> END
> RETURN res
> END Place;
>
> My proposal:
> k := j;
> WHILE (k <= size) & puzzl[k] DO INC(k) END;
> IF k > size THEN k := 0 END
> RETURN k
> END Place;
This certainly is an elegant simplification.
Thanks again!
--
Hans Klaver
More information about the Oberon
mailing list