Sea*_*ill 4 string parsing haskell
我需要能够编写一个函数来显示字符串中重复的单词并按顺序返回字符串列表并忽略非字母
例如,在拥抱提示
repetitions :: String -> [String]
repetitions > "My bag is is action packed packed."
output> ["is","packed"]
repetitions > "My name name name is Sean ."
output> ["name","name"]
repetitions > "Ade is into into technical drawing drawing ."
output> ["into","drawing"]
Run Code Online (Sandbox Code Playgroud)
要将字符串拆分为单词,请使用该words函数(在Prelude中).为了消除非单词字符,filter用Data.Char.isAlphaNum.将列表与其尾部一起压缩以获得相邻的对(x, y).折叠列表,建立一个包含所有xwhere x== 的新列表y.
喜欢:
repetitions s = map fst . filter (uncurry (==)) . zip l $ tail l
where l = map (filter isAlphaNum) (words s)
Run Code Online (Sandbox Code Playgroud)
我不确定它是否有效,但它应该给你一个粗略的想法.