开发者

Declare a function SML

How to declare a function suffixse开发者_如何学Pythonn : string list -> string list ?


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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜