在String:Haskell之间添加空格

时间:2017-11-21 17:36:13

标签: haskell

我试图在输入的字符串之间添加一个空格,虽然代码有效,但当我尝试将map函数与代码结合使用时,它会在遇到空白时给出模式匹配失败,任何我可以忽略空格或改进代码吗?

whiteSpace xs 
    | length xs <= 1        = xs
    | otherwise             = take 1 xs ++ " "++ whiteSpace (drop 1 xs)

1 个答案:

答案 0 :(得分:5)

您想实施Data.List.intersperse

吗?
> intersperse ' ' "asdfasd"
"a s d f a s d"

您的用例的基本实现可以是

> let white :: String -> String
|     white []     = []
|     white [x]    = [x]
|     white (x:xs) = x:' ':white xs