您如何在心理对话框中读取 OCaml 函数输入输出?

发布于 2024-11-18 03:59:53 字数 788 浏览 1 评论 0原文

对于[1]

# let make pair int (x:int) (y:int) = x,y ;;
val make_pair_int : int -> int -> int * int = <fun>

如何你读过打字信息输出吗?在其他语言中,我发现能够根据语法或打印输出构建一个令人信服的英语句子对于理解正在发生的事情至关重要。上述 OCaml 语句的句子是什么?

有人可以绘制出打印输出图,以便将打字信息与英语相关联吗?喜欢:

val make_pair_int : int -> int -> int * int = <fun>
      |              |   |  |   |  \_____/  \__/
      |              |   |  |   |     |       ` is a function
      |              |   |  |   |     ` a tuple of integers
      |              |   |  |   `
      |              |   |  `
      |              |   `
      |              `
      ` symbol make_pair_int is bound to

For [1]:

# let make pair int (x:int) (y:int) = x,y ;;
val make_pair_int : int -> int -> int * int = <fun>

How do you read the typing information output? In other languages, I find being able to construct a cogent, English sentence out of the syntax or the printouts to be critical in understanding what's going on. What's the sentence for the above OCaml statement?

Could someone diagram out the printout so as to correlate the typing information back into English? Like:

val make_pair_int : int -> int -> int * int = <fun>
      |              |   |  |   |  \_____/  \__/
      |              |   |  |   |     |       ` is a function
      |              |   |  |   |     ` a tuple of integers
      |              |   |  |   `
      |              |   |  `
      |              |   `
      |              `
      ` symbol make_pair_int is bound to

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

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

发布评论

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

评论(2

謌踐踏愛綪 2024-11-25 03:59:53

这只是函数的 柯里化 版本。对此类函数的阅读取决于您对函数的柯里化表示法的熟悉程度。

要点是考虑到采用两个参数采用一对参数是完全不同的概念,因为进行引用到特定类型构造函数(隐藏在 ocaml 代码片段中的 *, 符号下的内容)。

如果您不想引用配对(或元组,如果您想要任意参数数组)构造,您该怎么办?这个问题的柯里化答案是利用高阶函数:

val make_pair_int : int -> int -> int * int = <fun>
      |              |   |  |   |  \_____/  \__/
      |              |   |  |   |     |       ` and all that is a higher-order function
      |              |   |  |   |     ` a pair of integers
      |              |   |  |   `and returns
      |              |   |  `a function that takes an integer as an argument
      |              |   `and returns
      |              ` something that takes an integer as an argument 
      ` symbol make_pair_int is bound to

按照隐式惯例,这种柯里化解释(以及随之而来的符号)是公认的思考问题的方式。如果函数带有多个参数,您可以只计算类型中箭头的数量,并将最后一个箭头之前的箭头视为函数的“参数”。

This is just the curried version of your function. The reading of such a function depends on how comfortable you are with curried notation for functions.

The gist is to consider that taking two arguments is a completely different notion from taking a pair of arguments in the sense that the pair makes a reference to a specific type constructor (what is hidden under the * and , symbols in your ocaml snippet).

What do you do if you do not want to make a reference to a pairing (or tupling, if you want an arbitrary array of arguments) construct ? The curried answer to that question is the one that makes use of higher-order functions:

val make_pair_int : int -> int -> int * int = <fun>
      |              |   |  |   |  \_____/  \__/
      |              |   |  |   |     |       ` and all that is a higher-order function
      |              |   |  |   |     ` a pair of integers
      |              |   |  |   `and returns
      |              |   |  `a function that takes an integer as an argument
      |              |   `and returns
      |              ` something that takes an integer as an argument 
      ` symbol make_pair_int is bound to

With the implicit convention that this curried interpretation (and the notation that comes with it) is the admitted way of thinking of a function taking several arguments, you can just count the number of arrows in the type, and consider those before the last one as "the arguments" of your function.

内心激荡 2024-11-25 03:59:53

我想这是非常主观的,取决于功能的使用。通常我会尝试将其视为函数的非柯里化版本,然后在脑海中阅读。然而,如果我真的使用这个函数来构造闭包,我可能会把它理解为不同的东西。

因此,在这种情况下,根据我使用此函数的上下文,我会将其读为:

“从 int 和 int 到整数元组的函数”

在其他情况下,同样可能会变为:

“从 int 到从 int 到整数元组的函数” “

我想在同一函数的两种表示之间进行切换的能力为以后使用该函数提供了很大的灵活性。

I guess it is highly subjective and depends on the use of the function. Usually I try to read this as the uncurried version of the function and then read this in my mind. However if I actually use this function to construct closures I might read it as something different.

So in this case, depending on the context I use this function i would read it as:

"function from int and int to tuple of integers"

In other cases the same might become:

"function from int to function from int to tuple of integers"

I guess the ability to switch between the two representations of the same function offers a lot of flexibility in the use of the function later.

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