问题描述
我想知道是否可以将字符串转换为字符列表?
I was wondering if I can convert a string to a list of characters?
"jt5x=!" -> ["j","t","5","x","=","!"]
基本上是吗?
example :: String -> [Char]
推荐答案
(将评论收集为答案)
由于在haskell中,String
是一个字符列表,即[Char]
,只需返回给定的输入即可.
Because in haskell, a String
is a list of characters, i.e. [Char]
, just returning the input as given will do.
example = id
做您想要的.请注意,id
被定义为
does what you want. Note that id
is defined as
id x = x
您的示例"jt5x=!" -> ["j","t","5","x","=","!"]
与描述不符:双引号""
括起String
而不是单个Char
行为.对于字符,请使用单引号'
.您可以输入
Your example "jt5x=!" -> ["j","t","5","x","=","!"]
does not match the description: Double quotes ""
enclose String
s not single Char
acters. For characters use single quotes '
. You can type
"jt5x=!" == ['j','t','5','x','=','!']
进入GHCi,并看到它返回True
.键入map (:[]) "jt5x=!"
即可实际看到["j","t","5","x","=","!"]
.
into GHCi and see it returns True
. Type map (:[]) "jt5x=!"
to actually see ["j","t","5","x","=","!"]
.
这篇关于字符串到字符列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!