Created
May 17, 2013 09:25
-
-
Save chomado/5597994 to your computer and use it in GitHub Desktop.
でも今
# euclid' 0 4 2
って最初のcを0って指定してしか動かないので、できれば
euclid' 4 2
って感じで引数減らしたいんですけどどうすればいいのか分からない
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 目的 : 最大公約数を求める *) | |
let rec euclid m n = | |
if n = 0 then m | |
else if m = n then m | |
else if m > n then euclid n ( m mod n ) | |
else euclid n m | |
(* 目的: 最大公約数を求めて、何回引き算したか(=何回割り算したか) 求める *) | |
(* euclid' :: int -> int -> int -> int *) | |
let rec euclid' c m n = | |
if n = 0 then c | |
else if m >= n then euclid' (c + 1) n ( m - n ) | |
else euclid' c n m | |
let test1_euclid' = euclid' 0 4 2 = 2 | |
let test2_euclid' = euclid' 0 826 204 = 28 | |
(* | |
# #use "euclid.ml";; | |
val euclid : int -> int -> int = <fun> | |
val euclid' : int -> int -> int -> int = <fun> | |
val test1_euclid' : bool = true | |
val test2_euclid' : bool = true | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
値を交換するのは大小関係が入れ替わった時だけだよ〜