如何用Haskell替换字符串中的字母

时间:2016-02-11 01:48:13

标签: haskell duplicates

我必须创建名为markDups的Haskell函数来处理字符串,用下划线替换所有重复出现的字符," _",字符。

这是我到目前为止的代码。

makeBar :: Char -> [Char] -> [Char]
makeBar c  (x:xs) | c == x = '_':makeBar c xs --turn into a "_"
                  | otherwise = x:makeBar c xs--ignore and move on

当我运行它时,这是我的输出错误消息 enter image description here

输出应该是这样的

enter image description here

我该怎么办?

1 个答案:

答案 0 :(得分:1)

这似乎有效:

import Data.Set
main = putStrLn (markDups "hello world" empty)


markDups :: [Char] -> Set Char -> [Char]
markDups [] set = []
markDups (x:rest) set
 | member x set = '_':(markDups rest set)
 | otherwise    = x:(markDups rest (insert x set))