showcase23.mod 919 B

12345678910111213141516171819202122232425262728293031323334353637
  1. MODULE Showcase23;
  2. (* Showcase: a generic doubly linked list. DList(T) is instantiated as
  3. IntList = DList(INTEGER); the program builds a list, walks it with
  4. Get(i), reverses it and sums it. Prints, then exits with the sum. *)
  5. IMPORT SysIO, IntList;
  6. VAR l : IntList.List; i, sum, ExitCode : INTEGER;
  7. PROCEDURE Show(pfx : ARRAY OF CHAR);
  8. BEGIN
  9. SysIO.Write(pfx);
  10. i := 0;
  11. WHILE i < IntList.Count(l) DO
  12. SysIO.Write(" ");
  13. SysIO.WriteInt(IntList.Get(l, i));
  14. i := i + 1
  15. END;
  16. SysIO.WriteLn
  17. END Show;
  18. BEGIN
  19. IntList.Init(l);
  20. IntList.Append(l, 10);
  21. IntList.Append(l, 20);
  22. IntList.Prepend(l, 5);
  23. Show("list:");
  24. IntList.Reverse(l);
  25. Show("reversed:");
  26. sum := 0; i := 0;
  27. WHILE i < IntList.Count(l) DO
  28. sum := sum + IntList.Get(l, i);
  29. i := i + 1
  30. END;
  31. SysIO.Write("sum="); SysIO.WriteInt(sum); SysIO.WriteLn;
  32. ExitCode := sum (* 35 *)
  33. END Showcase23.