辅助GCD功能初学者

时间:2016-01-20 19:46:33

标签: f#

所以我有这个

// Greatest common divisor

    let rec gcd x y =
      if y = 0 then x
       else gcd y (x%y)

//multiplying fraction using tuples. So , instead of /

    let (.*)(a,b)(c,d)=((a*c)/(gcd (a*c) (b*d)),((d*b)/(gcd(a*c)(d*b))));;

//adding fractions using tuples and simplifying

    let (.+)(a,b)(c,d)=((a*d+c*b)/(gcd(a*d+c*b)(d*b)),(d*b)/(gcd(a*d+c*b)(d*b)));;

它完美无瑕,但我希望能够使用辅助功能来完成这项工作。

我有

let rec gcd = function | (a,0) -> a | (a,b) -> gcd (b, a % b)

,但我不熟悉如何让它发挥作用。每次我尝试实现它,它都不会调用gcd。我确定它是一个f#syntac的东西。也就是因为我对这种语言不熟悉并没有看到足够的例子。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

let rec gcd a b = 
    match a, b with
    | (a,0) -> a
    | (a,b) -> gcd b (a % b)
相关问题