ML 中类型表达式的查询

发布于 2024-09-10 09:22:52 字数 344 浏览 2 评论 0原文

所有,

这是我需要转换为 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 技术交流群。

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

发布评论

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

评论(1

无人问我粥可暖 2024-09-17 09:22:52

你得到一个 int 和一个函数 int*int -> '一个列表。您应该返回一个'a list。因此,您需要做的就是调用使用 (x,x) 获得的函数(其中 x 是您获得的 int)并返回结果。所以

fun foo x f = f (x,x)

请注意,这不是唯一可能的类型为 int -> 的函数。 (int*int -> '一个列表) -> '一个列表。例如,函数 fun foo xf = f (x, 42) 和 fun foo xf = f (23, x) 也具有该类型。

编辑:

为了使类型完全匹配,添加类型注释来限制 f 的返回类型:

fun foo x (f : int*int -> 'a list) = f (x,x)

但请注意,没有真正的理由这样做。此版本的行为与之前的版本完全相同,只是它只接受返回列表的函数。

You get an int and a function int*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. So

fun foo x f = f (x,x)

Note that this is not the only possible function with type int -> (int*int -> 'a list) -> 'a list. For example the functions fun foo x f = f (x, 42) and fun 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:

fun foo x (f : int*int -> 'a list) = f (x,x)

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.

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