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

时间:2011-02-20 08:22:14

标签: 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'

第二个版本出了什么问题?

2 个答案:

答案 0 :(得分:6)

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

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

printfn "%A" (swap_tuple ("one", "two") )

答案 1 :(得分:3)

作为Jimmy答案的替代方案,您还可以使用管道运算符:

printfn "%A" <| swap_tuple ("one", "two")

("one", "two")
|> swap_tuple
|> printfn "%A"