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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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#,我尝试使用模式匹配测试char类型元组中元素的相等性,如下所示: let swap (x,y) = match fst(x,y) with | snd(x,y) -> (x,y) | _ -> (y,x);; 并接收以下错误:stdin(11,8):错误FS0039:未定义模式鉴别器“snd” 请注意,我已经找到了解决实际问题的更好方法。我只是好奇为什么这种方法不起作用。它不起作用,因为snd(x,y)不是受支持的模式。您可以在此处看到整个表: 您可以改为使用变量模式,并在以下情况

我尝试使用模式匹配测试char类型元组中元素的相等性,如下所示:

let swap (x,y) =
match fst(x,y) with
| snd(x,y) -> (x,y)
| _ -> (y,x);;
并接收以下错误:
stdin(11,8):错误FS0039:未定义模式鉴别器“snd”


请注意,我已经找到了解决实际问题的更好方法。我只是好奇为什么这种方法不起作用。

它不起作用,因为
snd(x,y)
不是受支持的模式。您可以在此处看到整个表:

您可以改为使用变量模式,并在以下情况下检查
之后是否相等:

match x, y with
| x, y when x = y -> (x,y)
| _ -> (y,x);;
但是在这种情况下,如果。。然后。。否则

if x = y then (x,y) else (y,x)

它不起作用,因为
snd(x,y)
不是受支持的模式。您可以在此处看到整个表:

您可以改为使用变量模式,并在以下情况下检查
之后是否相等:

match x, y with
| x, y when x = y -> (x,y)
| _ -> (y,x);;
但是在这种情况下,如果。。然后。。否则

if x = y then (x,y) else (y,x)

它不起作用,因为您不能在匹配模式中使用函数。您可以像这样使用模式匹配:

let swap (x, y) =
    match x = y with
    | true -> (x, y)
    | _ -> (y, x)
或者只使用简单的if表达式:

let swap (x, y) = if x = y then (x, y) else (y, x)

它不起作用,因为您不能在匹配模式中使用函数。您可以像这样使用模式匹配:

let swap (x, y) =
    match x = y with
    | true -> (x, y)
    | _ -> (y, x)
或者只使用简单的if表达式:

let swap (x, y) = if x = y then (x, y) else (y, x)
旁白-
将fst(x,y)与
匹配可以写得更优雅。旁白-
将fst(x,y)与
匹配可以写得更优雅。