您好我是Haskell的新手,我在嘗試使此腳本運行時遇到問題。該腳本從命令行中讀取參數並在單獨的文本文件中找到它們。在文本文件輸出中查找並替換帶有星號的文字
E.G:cat.txt | ./red房屋大貓(在編譯器中)
它通過用星號(**)星號代替文本文件中的某些單詞。用於每個編輯單詞的星星數量應等於單詞中的字符數。
module Main where
import System
import Data.Char
import Data.List
lowercase :: String -> String
lowercase = map toLower
main = do
arg1 <- getArgs
txt <- getContents
putStr (redact txt arg1)
redact :: String -> String -> String
redact input xWords = unlines [ work line | line <- lines input ]
where work line = unwords [ foo word | word <- words line ]
foo w | lowercase(w) == lowercase(xWords) = convertWord w 1
| otherwise = w
convertWord Eq a => [a] -> [a]
convertWord = map (const '*')
然而,當我嘗試編譯此,GHCI返回錯誤:
redact.hs:13:38:
Couldn't match expected thye 'Char' with actual type '[Char]'
Expected type: String
Actual type: [String]
In the second argument of 'redact', namely 'arg1'
In the first of 'putStr', namely '<redact txt arg1>'
Failed, module loaded: none.
因此,代碼:
putStr (redact txt arg1)
引起的問題。
非常感謝您的幫助,如果您可以改進代碼,那將是非常棒的。
編輯:
我想進入儘可能多的ARGS越好,它不事關你多少ARGS進入,我想:
(arg1:arg2:arg3:arg4:arg5:_) <- getArgs
但我不得不進入EXACT 5個ARGS,不管我輸入多少參數都不重要。 我正在考慮使用某種循環,但我不確定?
再次感謝您的幫助。
根據示例,使用_all_參數是OP實際需要的。 – leftaroundabout 2012-03-29 22:51:05
@leftaroundabout:嗯,對。我被'arg1'的命名弄糊塗了。哦,希望我的其他建議對我有幫助。 – ehird 2012-03-29 22:52:22