我必须创建名为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
输出应该是这样的
我该怎么办?
答案 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))