[Oberon] Eliminating RETURN statements
Diego Sardina
dsar at eml.cc
Wed Apr 8 22:06:15 CEST 2020
On Wed, Apr 8, 2020, at 8:29 PM, August Karlstrom wrote:
> On 2020-04-08 01:30, Hans Klaver wrote:> (* Oberon-07, right output *)
>
> Here is an alternative:
>
> PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
> VAR k: INTEGER;
> BEGIN
> k := 0;
> WHILE (k <= piecemax[i]) & ~(p[i, k] & puzzl[j + k]) DO
> INC(k)
> END
> RETURN k > piecemax[i]
> END Fit;
>
>
This is the best way to write the algorithm, by putting the post-condition expression in the RETURN part.
I did something similar while porting Sets.mod to Oberon-07:
(* Oberon *)
PROCEDURE Includes*(VAR s1, s2: ARRAY OF SET): BOOLEAN;
VAR i: INTEGER;
BEGIN
i := 0;
WHILE i < LEN(s1) DO
IF s1[i] + s2[i] # s1[i] THEN RETURN FALSE END;
INC(i)
END;
RETURN TRUE;
END Includes;
(* Oberon-07 *)
PROCEDURE Includes*(s1, s2: ARRAY OF SET): BOOLEAN;
VAR i: INTEGER;
BEGIN
i := 0;
WHILE (i < LEN(s1)) & (s1[i] + s2[i] = s1[i]) DO INC(i) END;
RETURN i = LEN(s1);
END Includes;
The post-condition of the algorithm is (i = LEN(s1)) OR (s1[i] + s2[i] # s1[i]) but i = LEN(s1) suffice.
--
Diego Sardina
More information about the Oberon
mailing list