使用列表

时间:2013-01-05 01:03:16

标签: string haskell

我是初学者,并在haskell中编写hangman的实现。 必须在' * '中显示猜测的字符 - 就像字符串一样,目前我使用的是代码:

proceed char word progress = let zprog = zip progress [0..] in
    foldl (\a x -> a ++ [fst x]) "" $ map (f char word) zprog where    
        f c w el = 
            if c == w !! (snd el) then
                (c, snd el)
            else
                el

如果单词是“shadow”并且进度如“******”,则char ='s'函数返回“s ****”。

如何重写该功能?我看到它不干净的解决方案。 解决方案(@luqui):

proceed char = zipWith combine where
    combine x y
        | x == char = char
        | otherwise = y

(“word”和“progress”参数转移到zipWith,因为haskell的eta-reduction)

1 个答案:

答案 0 :(得分:2)

你想到zip是对的,你似乎已经做了一些额外的工作。看看你是否可以写一个合适的辅助函数combine

proceed char word progress = zipWith combine word progress
    where
    combine :: Char -> Char -> Char
    combine = ?