我是haskell的新故障,我在嘗試使此腳本正常工作時遇到了相當多的麻煩。其目的是讓它從命令行中讀取參數,並在單獨的文本文件中找到它們。然後用與該詞的長度相同量的星號代替這些詞。我已經設法讓它找到一個替代的話,但自此以後就已經碰壁了。我嘗試了各種各樣的方式,希望有人能夠爲我澄清它。查找並替換文本文件輸出中的單詞
module Main where
import System
import Data.Char
lowercase = map toLower
main = do (arg1:arg2:arg3:arg4:arg5:_) <- getArgs
txt <- getContents
putStr (redact txt arg1 arg2 arg3 arg4 arg5)
redact file w1 w2 w3 w4 w5 = unlines [ process line | line <- lines file ]
where process line = unwords [ f word | word <- words line ]
f w | lowercase(w) == lowercase(w1) = convertWord w 1
| lowercase(w) == lowercase(w2) = convertWord w 1
| lowercase(w) == lowercase(w3) = convertWord w 1
| lowercase(w) == lowercase(w4) = convertWord w 1
| lowercase(w) == lowercase(w5) = convertWord w 1
| otherwise = w
convertWord :: Eq a => [a] -> [a] -> [a] -> [a]
convertWord [] _ = []
convertWord word count = temp
where if count == 1 then let temp = ""
if count <= length(word)
then temp = temp ++ "*"
convertWord word count+1
的想法是,在convertWord部分稱爲並創建星號的字符串,以反饋至要被顯示在輸出中纂。然而,當我嘗試編譯此,GHC返回錯誤「redact.hs:28:13:解析錯誤(可能是不正確的縮進)」提前爲任何幫助
感謝您能提供
湯姆
一個提示,我強烈建議您嘗試避免代碼中的代碼重複。考慮如何將所有「小寫字母(w)==小寫字母(w1)= convertWord w 1 '警衛轉換爲單個。永遠不要複製代碼是一個好習慣。 :) – Tarrasch 2011-03-20 22:31:59
例如,你可以做這樣的事情:'小寫w \'elem \'映射小寫[w1,w2,w3,w4,w5]' – fuz 2011-03-21 07:07:48