The Modula-3 Program: TPK.m3

(* TPK.m3 -- Knuth's TPK program in Modula-3 *)

MODULE TPK EXPORTS Main;

IMPORT Math;         (* Math interface to C math library. *)
IMPORT IO;           (* Standard module for input/output. *)

(* f(x) = sqrt(|x|) + 5*x**3 *)
PROCEDURE f (x: LONGREAL): LONGREAL =
   BEGIN
      RETURN (Math.sqrt (ABS(x)) + 5.0D0*Math.pow(x,3.0D0));
   END f;

VAR
   A: ARRAY [0 .. 10] OF LONGREAL;

BEGIN
   (*  Read in the values of the array "A"  *)
   FOR i := FIRST(A) TO LAST(A) DO
      A[i] := FLOAT (IO.GetReal (), LONGREAL);
   END;

   (*  In reverse order, apply "f" to each element of "A" and print *)
   FOR i := LAST(A) TO FIRST(A) BY -1 DO
      WITH
         y = f (A[i])
      DO
         IO.PutInt (i);
         IO.Put ("  ");
         IF (y > 400.0D0) THEN
            IO.Put ("too large\n");
         ELSE
            IO.PutReal (FLOAT(y));
            IO.Put ("\n");
         END;
      END;
   END;
END TPK.