键入不匹配错误

时间:2011-06-24 11:40:35

标签: f#

这是我的代码:

open System

let rec gcd a b =
    match b with
        | x when x = 0 -> a
        | _ -> gcd(b, a % b)

let result = gcd 15 10

[<EntryPoint>]
let main(args : string[]) =
    printfn "result = %d" result 
    0

为什么我收到此代码的错误:

D:\datahub\Dropbox\development\myprojects\project-euler\Problem_5\problem_5.fs(6,16): error FS0001: Type mismatch. Expec
ting a
    'a
but given a
    int -> 'a
The resulting type would be infinite when unifying ''a' and 'int -> 'a'

1 个答案:

答案 0 :(得分:3)

该示例尝试使用逗号分隔参数。在F#中,通过用空格分隔多个参数来提供给函数:

let rec gcd a b =
    match b with
        | x when x = 0 -> a
        | _ -> gcd b (a % b)