F# 显式匹配 vs 函数语法 [英] F# explicit match vs function syntax
本文介绍了F# 显式匹配 vs 函数语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
抱歉标题模糊,但这个问题的一部分是这两种语法样式的名称:
Sorry about the vague title, but part of this question is what these two syntax styles are called:
let foo1 x =
match x with
| 1 -> "one"
| _ -> "not one"
let foo2 = function
| 1 -> "one"
| _ -> "not one"
另一部分是两者之间有什么区别,我什么时候想使用其中一个?
The other part is what difference there is between the two, and when I would want to use one or the other?
推荐答案
匹配版本称为模式匹配表达式".函数版本被称为模式匹配函数".在 spec 的第 6.6.4 节中找到.
The match version is called a "pattern matching expression". The function version is called a "pattern matching function". Found in section 6.6.4 of the spec.
两者兼而有之是一种风格问题.当我需要定义一个只是匹配语句的函数时,我更喜欢只使用函数版本.
Using one over the other is a matter of style. I prefer only using the function version when I need to define a function that is only a match statement.
这篇关于F# 显式匹配 vs 函数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文