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# match子句中的主动模式_F# - Fatal编程技术网

F# match子句中的主动模式

F# match子句中的主动模式,f#,F#,我正在努力更好地理解活动模式是如何工作的-如果我读错了活动模式,请纠正我,举下面的例子: let (|UpperCase|) (x:string) = x.ToUpper() let result = match "foo" with | UpperCase "FOO" -> true | _ -> false 我看我们在比较 (Uppercase "foo") with "FOO" 但在这种情况下,当我读到

我正在努力更好地理解活动模式是如何工作的-如果我读错了活动模式,请纠正我,举下面的例子:

let (|UpperCase|) (x:string) = x.ToUpper()

let result = match "foo" with
                 | UpperCase "FOO" -> true
                 | _ -> false
我看我们在比较

(Uppercase "foo") with "FOO"
但在这种情况下,当我读到

| UpperCase "Foo"
这段代码不是应该这样写吗

let result = match UpperCase "foo" with

有更好的阅读方法吗?

将匹配视为简化的if/else链。例如:

match "foo" with
| "foo" -> true    
| _ -> false
match "foo" with
| UpperCase "FOO" -> true
| _ -> false
let (|IsInt|IsString|) (x:obj) = match x with :? int -> IsInt | _ -> IsString

match someValue with
| IsInt -> true
| IsString -> false
可以是:

if "foo" = "foo" then true
else false
活动模式是隐式函数调用。在您的示例中:

match "foo" with
| "foo" -> true    
| _ -> false
match "foo" with
| UpperCase "FOO" -> true
| _ -> false
let (|IsInt|IsString|) (x:obj) = match x with :? int -> IsInt | _ -> IsString

match someValue with
| IsInt -> true
| IsString -> false
基本上是:

if (UpperCase "foo") = "FOO" then true
else false
您正在匹配通过调用推送foo的结果,您不必使用通常的函数调用语法来指定它

为了回答你的另一个问题,在这种特殊情况下,你完全可以这样做以达到同样的效果:

let UpperCase (x:string) = x.ToUpper()

match UpperCase "foo" with
| "FOO" -> true
| _ -> false
当您可以有多个您想要匹配的模式结果时,这就变得有点困难了,因为活动模式更有用

例如:

match "foo" with
| "foo" -> true    
| _ -> false
match "foo" with
| UpperCase "FOO" -> true
| _ -> false
let (|IsInt|IsString|) (x:obj) = match x with :? int -> IsInt | _ -> IsString

match someValue with
| IsInt -> true
| IsString -> false

将匹配视为简化的if/else链。例如:

match "foo" with
| "foo" -> true    
| _ -> false
match "foo" with
| UpperCase "FOO" -> true
| _ -> false
let (|IsInt|IsString|) (x:obj) = match x with :? int -> IsInt | _ -> IsString

match someValue with
| IsInt -> true
| IsString -> false
可以是:

if "foo" = "foo" then true
else false
活动模式是隐式函数调用。在您的示例中:

match "foo" with
| "foo" -> true    
| _ -> false
match "foo" with
| UpperCase "FOO" -> true
| _ -> false
let (|IsInt|IsString|) (x:obj) = match x with :? int -> IsInt | _ -> IsString

match someValue with
| IsInt -> true
| IsString -> false
基本上是:

if (UpperCase "foo") = "FOO" then true
else false
您正在匹配通过调用推送foo的结果,您不必使用通常的函数调用语法来指定它

为了回答你的另一个问题,在这种特殊情况下,你完全可以这样做以达到同样的效果:

let UpperCase (x:string) = x.ToUpper()

match UpperCase "foo" with
| "FOO" -> true
| _ -> false
当您可以有多个您想要匹配的模式结果时,这就变得有点困难了,因为活动模式更有用

例如:

match "foo" with
| "foo" -> true    
| _ -> false
match "foo" with
| UpperCase "FOO" -> true
| _ -> false
let (|IsInt|IsString|) (x:obj) = match x with :? int -> IsInt | _ -> IsString

match someValue with
| IsInt -> true
| IsString -> false

在您的示例中,您结合了两种:不带参数的大写单例和常量模式FOO。其效果实际上与在匹配表达式中应用函数|大写|的效果相同:

match "foo" with
| UpperCase "FOO" -> true
| _ -> false
// val it : bool = true

match (|UpperCase|) "foo" with
| "FOO" -> true
| _ -> false
// val it : bool = true
现在,将常量与常量进行匹配不是很通用,所以让我们制作一个函数

let isFooBarCaseInsensitive = function
| UpperCase "FOO" | UpperCase "BAR" -> true
| _ -> false
// val isFooBarCaseInsensitive : _arg1:string -> bool
isFooBarCaseInsensitive "foo"
// val it : bool = true
isFooBarCaseInsensitive "fred"
// val it : bool = false
模式不仅用于匹配关键字和函数关键字,还用于try…with、fun和最显著的let:


在您的示例中,您结合了两种:不带参数的大写单例和常量模式FOO。其效果实际上与在匹配表达式中应用函数|大写|的效果相同:

match "foo" with
| UpperCase "FOO" -> true
| _ -> false
// val it : bool = true

match (|UpperCase|) "foo" with
| "FOO" -> true
| _ -> false
// val it : bool = true
现在,将常量与常量进行匹配不是很通用,所以让我们制作一个函数

let isFooBarCaseInsensitive = function
| UpperCase "FOO" | UpperCase "BAR" -> true
| _ -> false
// val isFooBarCaseInsensitive : _arg1:string -> bool
isFooBarCaseInsensitive "foo"
// val it : bool = true
isFooBarCaseInsensitive "fred"
// val it : bool = false
模式不仅用于匹配关键字和函数关键字,还用于try…with、fun和最显著的let: