Comment detail

法演算 (Nested Flatten)
 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
27
28
29
module Law = struct
  (*normalize 10 (-1);; =>9 *)
  let normalize m x =
    let x = x mod m in
    if x<0 then m+x else x
    
  (* print 10 (+) '+' 7 3;; print 法 演算 演算文字 演算対象1,2 *)
  let print m f op x y = 
    let norm = normalize m in
    Printf.printf "%d %c %d = " x op y;
    
    let nx,ny = (norm x),(norm y) in
    let answer = (norm (f nx ny)) in
      if nx=x or ny=y
      then print_int answer
      else Printf.printf "%d %c %d = %d" nx op ny answer;
    print_newline ()
    
end;;

(*使用例*)
  let add = Law.print 10 (+) '+' in
    add 1 2;  add 7 3;  add 11 12;;

  let sub = Law.print 10 (-) '-' in
    sub 3 2;  sub 2 3;;

  let ( * ) = Law.print 10 ( * ) '*' in
    2*3;  11*12;  18*39;;

orじゃなくてandだった。

Index

Feed

Other

Link

Pathtraq

loading...