(* main.sml *) (* entry point *) structure Main :> MAIN = struct (* val numeralToInt : string -> int numeralToInt s tries to convert s to an integer, returning NONE if it fails, and SOME of the integer, if it succeeds. The numeral must be in base 10, may optionally be preceded by "+" to indicate a positive number, or "-"/"~" to indicate a negative number, and may be preceded and/or followed by whitespace *) fun numeralToInt s = case Int.scan StringCvt.DEC (Substring.getc) (Substring.full s) of NONE => NONE | SOME(n, t) => if Substring.isEmpty(StringCvt.skipWS Substring.getc t) then SOME n else NONE (* val error : string * string -> OS.Process.status issue an error message on the standard error output, and return failure *) fun error(prog, s) = (TextIO.output(TextIO.stdErr, prog ^ ": " ^ s ^ "\n"); OS.Process.failure) (* val main : string * string list -> OS.Process.status the entry point *) fun main (prog, [s]) = (case numeralToInt s of NONE => error(prog, "argument must be nonnegative integer") | SOME n => if n < 0 then error(prog, "argument must be nonnegative integer") else let val prs = Primes.primes n in app (fn n => (TextIO.output (TextIO.stdOut, Int.toString n); TextIO.output(TextIO.stdOut, "\n"))) prs; OS.Process.success end) | main (prog, _) = error(prog, "usage: " ^ prog ^ " num") end;