【发布时间】:2015-05-27 22:56:31
【问题描述】:
我很难为此想出一个好的标题。
这里是要点:
我有一个函数 (remIgnored) 打算从字符串列表中删除字符串。
module Main(main) where
import System.Environment
import Data.List
import Data.Char
getLines :: FilePath -> IO [String]
getLines path = do
ls <- readFile path
return (lines ls)
getWords :: [String] -> [String]
getWords ws = words (unlines ws)
remIgnored :: String -> [String] -> [String]
remIgnored _ [] = []
remIgnored x (y:ys) | x == y = remIgnored x ys
| otherwise = y : remIgnored x ys
main :: IO ()
main = do
args <- getArgs
let path = args !! 0
let ignore = args !! 1
ig <- getLines ignore
ls <- getLines path
let ig' = map (map toLower) (getWords ig)
let ls' = map (map toLower) (getWords ls)
let ls'' = sort ls'
putStrLn "Original Lines:\n"
mapM_ putStrLn ls
putStrLn "\nLower Cased Words:\n"
mapM_ putStrLn ls'
putStrLn "\nLower Cased Words + Sorted:\n"
mapM_ putStrLn ls''
putStrLn "\nLower Cased Words + Sorted + Sanitized:\n"
--Something nice goes here
(请忽略对列表进行细微更改的不断重复,我只是在破解一些东西,看看它们是如何工作的)。
基本上,我的问题是:我将如何将函数 remIgnored 应用于列表 ls'' 以及 ig' 中的每个元素?我整晚都在盯着类型错误,但进展甚微(即使是目前为止的情况也让我很烦恼)。
供参考,我的输入文件:
test.txt:
The quick brown fox jumps over the lazy dog
Peter picked a pail of pickled peppers
She sells sea shells by the sea shore
ignore.txt:
a
the
of
by
样本输出:
Original Lines:
The quick brown fox jumps over the lazy
Peter picked a pail of pickled peppers
She sells sea shells by the sea shore
Lower Cased Words:
the
quick
brown
fox
jumps
over
the
lazy
dog
peter
picked
a
pail
of
pickled
peppers
she
sells
sea
shells
by
the
sea
shore
Lower Cased Words + Sorted:
a
brown
by
dog
fox
jumps
lazy
of
over
pail
peppers
peter
picked
pickled
quick
sea
sea
sells
she
shells
shore
the
the
the
Lower Cased Words + Sorted + Sanitized:
【问题讨论】:
-
@Zeta,代码可以编译。我多次尝试粉碎
remIgnored和igls''给我带来了一些搞笑的问题。
标签: string list function haskell types