Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 如何在条件语句中断言元组_F# - Fatal编程技术网

F# 如何在条件语句中断言元组

F# 如何在条件语句中断言元组,f#,F#,给定元组: let tuple = (true, 1) 如何在条件语句中使用此元组? 大概是这样的: if tuple.first then //doesnt work 或 我不想这样做: let isTrue value = let b,_ = value b if isTrue tuple then // boring 有没有一种很好的方法可以在不创建单独函数的情况下计算条件中的元组值?该函数可以帮助您解决这个问题 返回元组的第一个元素 例如: let tuple =

给定元组:

let tuple = (true, 1)
如何在条件语句中使用此元组? 大概是这样的:

if tuple.first then //doesnt work

我不想这样做:

let isTrue value = 
   let b,_ = value
   b

if isTrue tuple then // boring
有没有一种很好的方法可以在不创建单独函数的情况下计算条件中的元组值?

该函数可以帮助您解决这个问题

返回元组的第一个元素

例如:

let tuple = (true, 1)
if fst tuple then
    //whatever
第二个元素也有一个

另一种选择是使用:


这可以让您匹配更复杂的场景,这是F#中非常强大的构造。查看中的元组模式以获取一些示例。

您要查找的函数是“fst”

“fst”将得到元组的前半部分。 “snd”将获得下半场

有关更多信息,请参见。

您可以使用以下功能:

if tuple |> fst then
    ...
let tuple = (true, 1)

let value = 
    match tuple with
    | (true, _) -> "fst is True"
    | (false, _) -> "fst is False"

printfn "%s" value
let v = (true, 3)
if fst v then "yes" else "no"
if tuple |> fst then
    ...