I have a fully functional redact program written in haskell.it replaces all the words that you input with starts.However i have a problem with the command line arguments.
If you type in:
cat poem.txt | redact word1 word2 word3
it only redacts word1
if you write
cat poem.txt | redact “word1 word2 word3”
it redacts all 3 words…this is probably some kind of mistake i did with the command line arguments…here is my code
module Main where
import System
import Data.Char
import Data.Bits
convertWord :: Eq a=> [a] -> String
convertWord = map (const '*')
lowercase :: [Char]->[Char]
lowercase ch = map toLower ch
redact :: String -> String -> String
redact text keywords = unlines(map unwords redactedtext)
where redactedtext = map processed text1
text1 = map words (lines text)
processed = map tobeconverted
keywords1 = words keywords
tobeconverted x | lowercase x `elem` map lowercase keywords1 = convertWord x
| otherwise = x
main = do
text <- getContents
(key:_) <- getArgs
let
result = redact text key
putStr (result)
The point is this:
Here you explicitly ignore everything but the first argument.
If redact function would take a list of keywords, you could just pass through the whole list of arguments you get from getArgs:
Note that this way, your redact function actually gets a bit easier, because you do not have to split words.
In addition, you whole program gets safer, because it does not abort when no arguments are given.