showcase23.mod 1016 B

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