Feeding tuple into function such as printfn

房东的猫 提交于 2019-12-03 11:21:10

You want

let tuple = ("Hello", "world")   
printfn "%s %s" <|| tuple

Notice the double || in <|| and not a single | in <|

See: MSDN <||

You can also do

let tuple = ("Hello", "world")
tuple
||> printfn "%s %s"

There are other similar operators such as |>, ||>, |||>, <|, <||, and <|||.

A idiomatic way to do it using fst and snd is

let tuple = ("Hello", "world")
printfn "%s %s" (fst tuple) (snd tuple)

The reason you don't usually see a tuple passed to a function with one of the ||> or <|| operators is because of what is known as a destructuring.

A destructing expression takes a compound type and destructs it into parts.

So for the tuple ("Hello", "world") we can create a destructor which breaks the tuple into two parts.

let (a,b) = tuple

I know this may look like a tuple constructor to someone new to F#, or may look even odder because we have two values being bound to, (noticed I said bound and not assigned), but it takes the tuple with two values and destructured it into two separate values.

So here we do it using a destructuring expression.

let tuple = ("Hello", "world")
let (a,b) = tuple
printfn "%s %s" a b

or more commonly

let (a,b) = ("Hello", "world")
printfn "%s %s" a b
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!