“fun”和“function”关键字有什么区别?

发布于 2024-08-08 10:58:42 字数 305 浏览 3 评论 0原文

代码

let (alt : recognizer -> recognizer -> recognizer) =
  fun a b p -> union  (a p) (b p)

有时我会看到类似或类似的

let hd = function
    Cons(x,xf) -> x
  | Nil -> raise Empty

funfunction 之间有什么区别?

Sometimes I see code like

let (alt : recognizer -> recognizer -> recognizer) =
  fun a b p -> union  (a p) (b p)

Or like:

let hd = function
    Cons(x,xf) -> x
  | Nil -> raise Empty

What is the difference between fun and function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫相离 2024-08-15 10:58:42

其语义与 F# 中的语义相同(可能是因为 F# 基于 OCaml):

  • function 允许使用模式匹配(即 |),但是因此它只能传递一个参数。

    函数 p_1 -> exp_1 | …… | p_n->指数n
    

    相当于

    有趣的体验 ->将 exp 与 p_1 匹配 -> exp_1 | …… | p_n->指数n
    
  • fun不允许模式匹配,但可以传递多个参数,例如

    有趣的 xy -> x + y
    

当可以使用两种形式中的任何一种时,fun 通常是由于其紧凑性而受到青睐。

另请参阅有关函数的 OCaml 文档

The semantics for this is the same as in F# (probably because F# is based on OCaml):

  • function allows the use of pattern matching (i.e. |), but consequently it can be passed only one argument.

    function p_1 -> exp_1 | … | p_n -> exp_n
    

    is equivalent to

    fun exp -> match exp with p_1 -> exp_1 | … | p_n -> exp_n
    
  • fun does not allow pattern matching, but can be passed multiple arguments, e.g.

    fun x y -> x + y
    

When either of the two forms can be used, fun is generally preferred due to its compactness.

See also OCaml documentation on Functions.

甜味拾荒者 2024-08-15 10:58:42

我的想法

function patterns

是“模式”的简写

(fun x -> match x with patterns)

,例如

| Some(x) -> yadda | None -> blah

(并且

fun args -> expr

是定义 lambda 的方式。)

The way I think about it

function patterns

is shorthand for

(fun x -> match x with patterns)

where 'patterns' is e.g.

| Some(x) -> yadda | None -> blah

(And

fun args -> expr

is how you define a lambda.)

凡尘雨 2024-08-15 10:58:42

Russ Cam 的回答是正确的。

这是 OCaml 列表上的一个帖子,讨论它

http://caml.inria.fr/pub/ml-archives/ocaml-beginners/2003/11/b8036b7a0c1d082111d7a83c8f6dbfbb.en.html

function 只允许一个参数,但允许对于模式匹配,而 fun 是定义函数的更通用和灵活的方式。

我通常使用 fun ,除非有充分的理由使用 function 。

您可以在您发布的代码中看到这一点,其中 fun 声明采用 3 个参数,而 function 声明对其输入进行模式匹配

Russ Cam is correct in his answer.

Here is a posting on the OCaml list talking about it

http://caml.inria.fr/pub/ml-archives/ocaml-beginners/2003/11/b8036b7a0c1d082111d7a83c8f6dbfbb.en.html

function only allows for one argument but allows for pattern matching, while fun is the more general and flexible way to define a function.

I generally use fun unless there is a good reason to use function.

You can see this in the code you posted where the fun declaration takes 3 arguments and the function declaration does pattern matching on it's input

埋情葬爱 2024-08-15 10:58:42
fun x1 ... xn -> e

是一个缩写

function x1 -> ... -> function xn -> e
fun x1 ... xn -> e

is an abbreviation for

function x1 -> ... -> function xn -> e
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文