应用字符串类型的函数->;[字符串]->;[String]到两个[String]s
我努力想为这部电影找到一个好的标题 要点如下: 我有一个函数(应用字符串类型的函数->;[字符串]->;[String]到两个[String]s,string,list,function,haskell,types,String,List,Function,Haskell,Types,我努力想为这部电影找到一个好的标题 要点如下: 我有一个函数(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)
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
ignore.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
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:
实际上,你应该使用折叠来实现这一点
remAllIgnored::[String]->[String]->[String]
remAllIgnored=翻转$foldr remIgnored
是的,就这么简单
如果您还不习惯折叠,您可以使用忽略列表的递归显式编码:
remAllIgnored [] words = ... -- nothing to ignore means: ?
remAllIgnored (ign0:igns) words
= let ?? -- somehow you need to 1. apply the `ign0`-ignore-patch
in ?? -- 2. process the rest of the `igns`
试试看,如果有什么特别的问题,你会发现。事实上,你应该使用折叠来解决
remAllIgnored::[String]->[String]->[String]
remAllIgnored=翻转$foldr remIgnored
是的,就这么简单
如果您还不习惯折叠,您可以使用忽略列表的递归显式编码:
remAllIgnored [] words = ... -- nothing to ignore means: ?
remAllIgnored (ign0:igns) words
= let ?? -- somehow you need to 1. apply the `ign0`-ignore-patch
in ?? -- 2. process the rest of the `igns`
试试看,如果有什么特别的问题,你会回来的。@Zeta,目前的代码是编译的。我多次尝试粉碎
remIgnored
和ig
ls'
都给我带来了一些可笑的问题。@Zeta,目前的代码是编译的。我试图粉碎remIgnored
和ig
ls'
的各种尝试都给我带来了一些可笑的问题。这两个问题我都不太明白。remAllIgnored
实际上是如何做的?(在第一种情况下,使用foldr
)它起作用了,但您介意解释一下实际发生的情况吗?在这个阶段,Haskell中的一切对我来说都像是黑魔法。好吧,为了理解折叠,你应该实现递归版本,它毕竟是等价的。恐怕你真的得自己去做,才能正确地理解它。至少试一下——你会被困在哪里?步骤[]
,步骤1.
或步骤2.
?@capncoolio也许foldr
版本更易于理解,采用了有意义的风格,即remalignored-toIgnore-words=foldr-remIgnored-words-toIgnore
。这大致扩展到(remIgnored toIgn1(…(remIgnored toIgnN words))
,其中所有被忽略的单词都将从单词列表中逐个删除。@capncoolio您以前使用过foldr
?我觉得这张图很好地解释了它的工作原理foldr
将其给定的列表(在图像的左侧)转换为右侧的嵌套函数调用:。它用f
调用“替换”列表的:
s,用z
替换空列表。左边的列表是书写[1,2,3,4,5]
的漫长过程。用手写出一个小例子会有帮助。这两个我都不太懂。remAllIgnored
实际上是如何做的?(在第一种情况下,使用foldr
)它起作用了,但您介意解释一下实际发生的情况吗?在这个阶段,Haskell中的一切对我来说都像是黑魔法。好吧,为了理解折叠,你应该实现递归版本,它毕竟是等价的。恐怕你真的得自己去做,才能正确地理解它。至少试一下——你会被困在哪里?步骤[]
,步骤1.
或步骤2.
?@capncoolio也许foldr
版本更易于理解,采用了有意义的风格,即remalignored-toIgnore-words=foldr-remIgnored-words-toIgnore
。这大致扩展到(remIgnored toIgn1(…(remIgnored toIgnN words))
,其中所有被忽略的单词都将从单词列表中逐个删除。@capncoolio您以前使用过foldr
?我觉得这张图很好地解释了它的工作原理foldr
将其给定的列表(在图像的左侧)转换为右侧的嵌套函数调用:。它用f
调用“替换”列表的:
s,用z
替换空列表。左边的列表是书写[1,2,3,4,5]
的漫长过程。手工写出一个小例子会有帮助。