此函数中存在奇怪的(?)类型不匹配错误

Pau*_*zak 1 f#

有人可以解释为什么编译器给我这个错误

类型不匹配.期待'a [] - >字符串,
但给出'a [] - >'a []
类型'字符串'与类型''a []'不匹配

在此代码段上:

let rotate s: string = 
  [|for c in s -> c|] 
  |> Array.permute (function | 0 -> (s.Length-1) | i -> i-1)
Run Code Online (Sandbox Code Playgroud)

而下面的那个编译得很好:

let s = "string"
[|for c in s -> c|] 
|> Array.permute (function | 0 -> (s.Length-1) | i -> i-1)
Run Code Online (Sandbox Code Playgroud)

mar*_*ski 5

您的第一个片段定义rotate了返回类型为的函数string.

尝试将其更改为:

let rotate (s: string) = 
  [|for c in s -> c|] 
  |> Array.permute (function | 0 -> (s.Length-1) | i -> i-1)
Run Code Online (Sandbox Code Playgroud)

在这种形式中,您定义一个带有一个字符串参数的函数(我想这就是您想要的)和推断的返回类型.