susu #4941(2007/12/23 15:17 GMT) [ OCaml ] Rating0/0=0.00
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;;
Rating0/0=0.00-0+
1 reply [ reply ]
susu #4942(2007/12/23 15:30 GMT) Rating0/0=0.00
orじゃなくてandだった。
[ reply ]
susu
#4941()
[
OCaml
]
Rating0/0=0.00
Rating0/0=0.00-0+
1 reply [ reply ]