ML 中类型表达式的查询
所有,
这是我需要转换为 ML 表达式的类型表达式:
int -> (int*int -> 'a list) -> 'a list
现在我知道这是一个带有 2 个参数的柯里化风格表达式: 第一个参数 = 类型 int 第二个参数 = 函数,它接受前一个 int 值两次并返回任何类型的列表
我很难计算出这样一个需要 int
并返回的函数“列表”
。
我是机器学习新手,因此这对其他人来说可能微不足道,但显然对我来说不是。
非常感谢任何帮助。
All,
Here is the type expression which I need to convert to a ML expression:
int -> (int*int -> 'a list) -> 'a list
Now I know this is a currying style expression which takes 2 arguments:
1st argument = Type int
and 2nd argument = Function which takes the previous int value twice and return a list of any type
I am having a hard time figuring such a function that would take an int
and return 'a list
.
I am new to ML and hence this might be trivial to others, but obviously not me.
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你得到一个
int
和一个函数int*int -> '一个列表
。您应该返回一个'a list
。因此,您需要做的就是调用使用 (x,x) 获得的函数(其中 x 是您获得的 int)并返回结果。所以请注意,这不是唯一可能的类型为 int -> 的函数。 (int*int -> '一个列表) -> '一个列表。例如,函数 fun foo xf = f (x, 42) 和 fun foo xf = f (23, x) 也具有该类型。
编辑:
为了使类型完全匹配,添加类型注释来限制 f 的返回类型:
但请注意,没有真正的理由这样做。此版本的行为与之前的版本完全相同,只是它只接受返回列表的函数。
You get an
int
and a functionint*int -> 'a list
. You're supposed to return an'a list
. So all you need to do is call the function you get with (x,x) (where x is the int you get) and return the result of that. SoNote that this is not the only possible function with type
int -> (int*int -> 'a list) -> 'a list
. For example the functionsfun foo x f = f (x, 42)
andfun foo x f = f (23, x)
would also have that type.Edit:
To make the type match exactly add a type annotation to restrict the return type of f:
Note however that there is no real reason to do that. This version behaves exactly as the one before, except that it only accepts functions that return a list.