[Oberon] Tidyness of iteration.
Alexander Ilin
ajsoft at yandex.ru
Mon Nov 14 19:58:19 CET 2016
14.11.2016, 21:18, "peter at easthope.ca" <peter at easthope.ca>:
> (* Pre-2013. *)
> PROCEDURE LOOPiteration();
> BEGIN
> Texts.OpenReader(R, t, 0);
> LOOP
> Texts.Read(R, ch);
> IF R.eot THEN EXIT ELSE (* Process ch *) END
> END
> END LOOPinteration.
>
> Project Oberon 2013, Page 5.
> "2. The LOOP and EXIT statements (repetitions with multiple exit points) have been discarded."
>
> (* Using REPEAT, R.eot must be checked twice in each iteration. *)
> PROCEDURE REPEATiteration();
> BEGIN
> Texts.OpenReader(R, t, 0);
> REPEAT
> IF ~R.eot THEN Texts.Read(R, ch); (* Process ch *) END
> UNTIL R.eot
> END REPEATinteration.
>
> (* Using WHILE, Texts.Read is duplicated. *)
> PROCEDURE WHILEiteration();
> BEGIN
> Texts.OpenReader(R, t, 0);
> Texts.Read(R, ch);
> WHILE ~R.eot
> (* Process ch *);
> Texts.Read(R, ch)
> END
> END WHILEinteration.
>
> Can the REPEAT example or the WHILE example be simplified further?
> (One must wonder about discarding REPEAT and WHILE and retaining LOOP with
> strictly one EXIT.)
The REPEATiteration should be as follows:
PROCEDURE REPEATiteration();
BEGIN
Texts.OpenReader(R, t, 0);
REPEAT
Texts.Read(R, ch);
IF ~R.eot THEN (* Process ch *) END
UNTIL R.eot
END REPEATiteration;
To simplify the code, why not return R.eot from the Texts.Read?
PROCEDURE WHILEiteration();
BEGIN
Texts.OpenReader(R, t, 0);
WHILE Texts.Read(R, ch)
(* Process ch *)
END
END WHILEiteration;
---=====---
Александр
More information about the Oberon
mailing list