您如何在心理对话框中读取 OCaml 函数输入输出?
对于[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是函数的 柯里化 版本。对此类函数的阅读取决于您对函数的柯里化表示法的熟悉程度。
要点是考虑到采用两个参数与采用一对参数是完全不同的概念,因为对进行引用到特定类型构造函数(隐藏在 ocaml 代码片段中的
*
和,
符号下的内容)。如果您不想引用配对(或元组,如果您想要任意参数数组)构造,您该怎么办?这个问题的柯里化答案是利用高阶函数:
按照隐式惯例,这种柯里化解释(以及随之而来的符号)是公认的思考问题的方式。如果函数带有多个参数,您可以只计算类型中箭头的数量,并将最后一个箭头之前的箭头视为函数的“参数”。
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:
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.
我想这是非常主观的,取决于功能的使用。通常我会尝试将其视为函数的非柯里化版本,然后在脑海中阅读。然而,如果我真的使用这个函数来构造闭包,我可能会把它理解为不同的东西。
因此,在这种情况下,根据我使用此函数的上下文,我会将其读为:
“从 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.