[英]String and add special characters from a list in Haskell
In Haskell I used to use filter to strip set of numbers from a list. 在Haskell中,我曾经使用过滤器从列表中剥离数字集。 However, I have not been able to make it working with this particular case.
但是,我无法使其适用于这种特殊情况。
I have a list of strings as follow: 我有一个字符串列表,如下所示:
["A","B","C","D","E","F","A","B","N"]
I want to string the []
and ""
so the final string is (with spaces): 我想将
[]
和""
字符串化,所以最后一个字符串是(带空格):
A B C D E F A B N
shouldn't a simple filter like print filter([) ["A","B","C","D","E","F","A","B","N"]
remove the [
? 不应使用像
print filter([) ["A","B","C","D","E","F","A","B","N"]
这样的简单过滤print filter([) ["A","B","C","D","E","F","A","B","N"]
删除[
?
update: 更新:
I read over this document and was able to get the following result: 我阅读了这份文件 ,并得到以下结果:
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"
However, when I use this : 但是,当我使用这个:
-- 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
I get this error 我得到这个错误
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))
unwords
works fine: unwords
正常工作:
λ> unwords ["A","B","C","D","E","F","A","B","N"]
"A B C D E F A B N"
Additionally, Data.List.intersperse
would help, along with concat
. 另外,
Data.List.intersperse
和concat
也会有所帮助。
import Data.List
solution :: [String] -> String
solution = concat . intersperse " "
What this does is separate each value in the list with " "
, then join (concatenate) the lists together. 这是用
" "
分隔列表中的每个值,然后将列表连接(连接)在一起。
If you want to separate with ", "
, you can easily change the above function: 如果要用
", "
分隔,则可以轻松更改上述功能:
solution :: [String] -> String
solution = concat . intersperse ", "
so that: 以便:
λ> solution ["A","B","C","D","E","F","A","B","N"]
"A, B, C, D, E, F, A, B, N"
λ> putStrLn $ solution ["A","B","C","D","E","F","A","B","N"]
A, B, C, D, E, F, A, B, N
To put this into the context of your IO: 要将其放入您的IO上下文中:
main = do
x <- getLine
putStrLn $ solution $ createalphs $ words x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.