1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
functor Modular (val m : int) = struct
  infix 7 *
  infix 6 + -

  local
    fun calc f x y s =
      let
        open Int

        val _ = print (toString x ^ " " ^ s ^ " " ^ toString y ^ " = ")
        val x' = x mod m and y' = y mod m
        val _ = if x >= m orelse y >= m then
                  print (toString x' ^ " " ^ s ^ " " ^ toString y' ^ " = ")
                else ()
        val result = f (x', y') mod m
      in
        print (toString result ^ "\n");
        result
      end
  in
    fun x + y = calc Int.+ x y "+"
    fun x - y = calc Int.- x y "-"
    fun x * y = calc Int.* x y "*"
  end

end