Haskell非数字字符串列表到不同的列表

时间:2011-04-15 19:11:46

标签: string list haskell

我有一个列表['%','&','/']。如何将其转换为[%,&,/]?

形式

2 个答案:

答案 0 :(得分:6)

myShow :: [Char] -> String
myShow s = concat ["[", intersperse ',' s, "]"]

像这样使用:

putStrLn (myShow ['%','&','/'])     -- prints [%,&,/]

但是如果您希望这与showprint一起使用,则必须定义自己的类型:

data MyChar = MyChar Char

instance Show MyChar
  where show (MyChar ch) = [ch]

然后按[MyChar]而不是[Char]

进行操作
let myList = map MyChar ['%','&','/']
-- ... do whatever you want with myList ...
print myList                        -- prints [%,&,/]

答案 1 :(得分:1)

"[" ++ intercalate ',' list ++ "]"

intercalateData.List中声明。