【发布时间】:2015-04-10 19:34:28
【问题描述】:
在 Haskell 中,我曾经使用过滤器从列表中删除一组数字。但是,我无法使其适用于这种特殊情况。
我有一个字符串列表如下:
["A","B","C","D","E","F","A","B","N"]
我想将[] 和"" 串起来,所以最后的字符串是(带空格):
A B C D E F A B N
不应该像print filter([) ["A","B","C","D","E","F","A","B","N"] 这样的简单过滤器删除[ 吗?
更新:
我阅读了this document 并且能够得到以下结果:
let example = (concat (intersperse " " ["A","B","C","D","E","F","A","B","N"]))
print example
-- this prints "A B C D E F A B N"
但是,当我使用这个时:
-- where createalphs create a list of strings
-- and userinput is a string entered by the user
let setofalph = ($ createalphs $ words userinput)
let example = (concat (intersperse " " setofalph))
print example
我收到这个错误
Couldn't match expected type `[[Char]]'
In the second argument of `intersperse', namely `setofalph'
In the first argument of `concat', namely
`(intersperse " " setofalph)'
In the expression: (concat (intersperse " " setofalph))
【问题讨论】:
-
["A","B","C","D","E","F","A","B","N"]是您的字符串,即"["A","B","C","D","E","F","A","B","N"]",还是字符串列表?你很不清楚。 -
@gspr 抱歉,这是一个字符串列表
-
对,那么“剥离 []”就没有意义了!这只是列表的符号。相反,您是否想将字符串列表转换为所有元素以逗号分隔的字符串?
-
你的
setofalph需要一个函数调用它像这样let example = setofalph unwords,你可能想要let setofalph = createalphs $ words userinput而不是let setofalph = ($ createalphs $ words userinput)