F#函数类型不匹配

pra*_*pes 1 f# types

我的测试功能出了什么问题?

let divisorOf(d, n) = n % d = 0

let notDivisible(d, n) = not (divisorOf(d, n))

let rec test(a, b, c) = function
  | (a, b, _) when (a > b) -> true
  | (a, b, c) -> notDivisible(a, c) && test(a + 1, b, c)
Run Code Online (Sandbox Code Playgroud)

我收到一个编译错误,第7行的表达式有函数类型,而不是bool.

(7,40): error FS0001: This expression was expected to have type
    bool    
but here has type
    'a * 'a * 'b -> bool    
Run Code Online (Sandbox Code Playgroud)

Joh*_*mer 5

当您使用关键字时,function您正在创建一个implict lambda.据推断,对此的输入是a int*int*int.要解决这个问题,只需要改变

let rec test(a,b,c) =
Run Code Online (Sandbox Code Playgroud)

let rec test =
Run Code Online (Sandbox Code Playgroud)

如果你想明确参数,你也可以把它写成

let rec test(d, e, f) = match (d,e,f) with //change letters to avoid variable hiding
  | (a, b, _) when (a > b) -> true
  | (a, b, c) -> notDivisible(a, c) && test(a + 1, b, c)
Run Code Online (Sandbox Code Playgroud)