Alex F
Alex F

Reputation: 43311

Type mismatch compilation error with tuple

The Tour of f# has a sample similar to this code:

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

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

So the code above work. But why does this code:

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

throws this compilation error:

error FS0001: 
Type mismatch. Expecting a     'a -> 'b -> 'c  but given a   'a -> unit     
The type ''a -> 'b' does not match the type 'unit'

What is wrong in the second version?

Upvotes: 1

Views: 389

Answers (2)

Frank
Frank

Reputation: 2788

As an alternative to Jimmy's answer, you could also use the pipeline operators:

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

or

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

Upvotes: 3

Jimmy
Jimmy

Reputation: 6171

In your second version, your format string has just the one format specifier, but the printfn statement has been suppplied with two. You need to use () to group together swap_tuple with its arguments into a single argument.

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

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

Upvotes: 6

Related Questions