使用元组键入不匹配编译错误

Ale*_*x F 0 f#

这段代码是完整的:

let swap_tuple (a, b) = (b, a)

let result = swap_tuple ("one", "two") 
printfn "%A" result

此代码提供编译错误:

let swap_tuple (a, b) = (b, a)

printfn "%A" swap_tuple ("one", "two") 
error FS0001: Type mismatch. Expecting a     'a -> 'b -> 'c     but given a     'a -> unit     The type ''a -> 'b' does not match the type 'unit'

第二个版本有什么问题?

Jim*_*mmy 6

在第二个版本中,您的格式字符串只有一个格式说明符,但printfn语句已经提供了两个.您需要使用()将swap_tuple及其参数组合到一个参数中.

let swap_tuple (a, b) = (b, a)

printfn "%A" (swap_tuple ("one", "two") )
Run Code Online (Sandbox Code Playgroud)