F#显式匹配vs函数语法 [英] F# explicit match vs function syntax

查看:69
本文介绍了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?

推荐答案

匹配版本称为模式匹配表达式".该功能版本称为模式匹配功能".在规范的第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.

一个使用另一个是一个风格问题.我只想在需要定义仅是match语句的函数时才使用函数版本.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆