声明一个函数 SML

发布于 2024-09-27 00:03:28 字数 49 浏览 0 评论 0原文

如何声明函数 suffixsen : string list ->字符串列表?

How to declare a function suffixsen : string list -> string list ?

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

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

发布评论

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

评论(2

何时共饮酒 2024-10-04 00:03:28

在括号内声明类型后,在外部使用 :return-type 声明函数的返回类型。至少在SMLnj是这样。我通过反复试验发现了这一点,找不到它的文档。

fun suffixson (xs: string list ): string list =
    map (fn x => x ^ "son") xs

After declaring types inside the parens, declare the function's return type on the outside with :return-type. At least in SMLnj. I found this through trial and error, can't find documentation for it.

fun suffixson (xs: string list ): string list =
    map (fn x => x ^ "son") xs
短叹 2024-10-04 00:03:28

在 sml 中定义带有一个参数的函数的语法是:

fun functionName argumentName = functionBody

或者

fun functionName (argumentName : argumentType) = functionBody

如果您想显式指定类型。因此,要定义一个名为 suffixsen 的函数,其类型为 string list ->字符串列表,您可以执行以下操作:

fun suffixsen (strings : string list) = someExpressionThatReturnsAStringList

编辑以响应您的评论:

为了将“son”附加到列表中的每个字符串,您应该查看 ^ 运算符[1],它连接字符串和 map 函数 对列表中的每个元素执行操作。

[1] http://www.standardml.org/Basis/string.html#SIG:STRING.^:VAL (复制此链接并将其粘贴到您的浏览器中 - 由于某种原因我无法使其可点击)

The syntax to define a function with one argument in sml is:

fun functionName argumentName = functionBody

or

fun functionName (argumentName : argumentType) = functionBody

if you want to specify the type explicitly. So to define a function named suffixsen of type string list -> string list, you can do:

fun suffixsen (strings : string list) = someExpressionThatReturnsAStringList

Edit in response to you comment:

In order to append "son" to each string in the list, you should look at the ^ operator[1], which concatenates string, and the map function which performs an operation for each element in a list.

[1] http://www.standardml.org/Basis/string.html#SIG:STRING.^:VAL (copy and paste this link in your browser - for some reason I can't get this to be clickable)

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