在SML中将字符串与文件夹连接 [英] Concatenating strings with foldr in SML
问题描述
我正在尝试声明一个函数,字符串列表->字符串,例如使用
[ Chicago, city, USA]
应该返回美国芝加哥城市
。到目前为止,我所做的是:
I'm trying to declare a function, string list -> string, that with the input for instance
["Chicago","city","USA"]
should return "Chicago city USA"
. What I did so far was this:
fun gather ts = foldr op ^ "" ts;
这似乎有些相似,但是问题是,我想包括空格在单词之间,因为此函数将返回 ChigagocityUSA
。
This seems to be somewhat along the lines, however the problem is, I would like to include the spaces between the words, as this function would return "ChigagocityUSA"
.
推荐答案
是的,问题在于 ^
是一个函数,它为两个字符串 foo和 bar返回 foobar,尽管您希望使用 foo bar。
Yes, the problem is that ^
is a function that for two strings "foo" and "bar" returns "foobar", although you want "foo bar".
因此,您需要定义一个函数,该函数接受两个字符串参数(作为一个元组),并返回两个字符串,且两个字符串之间有一个空格(因此 string1 ^ ^ string2
)。
So what you need to do is to define a function that takes two string arguments (as a tuple) and returns the two strings with a space in between them (so string1 ^ " " ^ string2
).
然后您可以将该函数作为 foldr的参数
并获得所需的结果。
You can then give that function as an argument to foldr
and get the result you want.
这篇关于在SML中将字符串与文件夹连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!