簡體   English   中英

顯示在haskell中重復的單詞列表

[英]Show a list of words repeated in 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"]

要將字符串拆分為單詞,請使用words function(在Prelude中)。 要消除非單詞字符,請使用Data.Char.isAlphaNum filter 將列表與其尾部一起壓縮以獲得相鄰的對(x, y) 折疊列表,建立一個包含x == y所有x的新列表。

喜歡:

repetitions s = map fst . filter (uncurry (==)) . zip l $ tail l
  where l = map (filter isAlphaNum) (words s)

我不確定它是否有效,但它應該給你一個粗略的想法。

我是這種語言的新手,所以我的解決方案在Haskell退伍軍人眼中可能是一種丑陋,但無論如何:

let repetitions x = concat (map tail (filter (\x -> (length x) > 1) (List.group (words (filter (\c -> (c >= 'a' && c <= 'z') || (c>='A' && c <= 'Z') ||  c==' ') x)))))

這部分將刪除字符串s中的所有非字母和非空格:

filter (\c -> (c >= 'a' && c <= 'z') || (c>='A' && c <= 'Z') ||  c==' ') s

這個將字符串s拆分為單詞並將相同的單詞組合成列表返回列表列表:

List.group (words s)

當此部分將刪除少於兩個元素的所有列表:

filter (\x -> (length x) > 1) s

之后,我們將所有列表連接到一個從中移除一個元素的列表

concat (map tail s)

這可能是不合理的,但它在概念上非常簡單。 我假設它正在尋找像示例一樣的連續重復單詞。

-- a wrapper that allows you to give the input as a String
repititions :: String -> [String]
repititions s = repititionsLogic (words s)
-- dose the real work 
repititionsLogic :: [String] -> [String]
repititionsLogic [] = []
repititionsLogic [a] = []
repititionsLogic (a:as) 
    | ((==) a (head as)) = a : repititionsLogic as
    | otherwise = repititionsLogic as

以Alexander Prokofyev回答的為基礎:

repetitions x = concat (map tail (filter (\\x -> (length x) > 1) (List.group (word (filter (\\c -> (c >= 'a' && c <= 'z') || (c>='A' && c <= 'Z') || c==' ') x)))))

刪除不必要的括號:

repetitions x = concat (map tail (filter (\\x -> length x > 1) (List.group (word (filter (\\c -> c >= 'a' && c <= 'z' || c>='A' && c <= 'Z' || c==' ') x)))))

使用$刪除更多括號(如果結束括號位於表達式的末尾,則每個$可以替換左括號):

repetitions x = concat $ map tail $ filter (\\x -> length x > 1) $ List.group $ word $ filter (\\c -> c >= 'a' && c <= 'z' || c>='A' && c <= 'Z' || c==' ') x

用Data.Char中的函數替換字符范圍,合並concat和map:

repetitions x = concatMap tail $ filter (\\x -> length x > 1) $ List.group $ word $ filter (\\c -> isAlpha c || isSeparator c) x

使用一個部分並以無點樣式進行曲線處理以簡化(\\x -> length x > 1) to ((>1) . length) ) in a right-to-left pipeline. 這將length與(> 1)(部分應用的運算符或 )組合在一個從右到左的管道中。

repetitions x = concatMap tail $ filter ((>1) . length) $ List.group $ word $ filter (\\c -> isAlpha c || isSeparator c) x

消除顯式“x”變量以使整個表達式無點:

repetitions = concatMap tail . filter ((>1) . length) . List.group . word . filter (\\c -> isAlpha c || isSeparator c)

現在整個函數,從右到左閱讀,是一個管道,只過濾字母或分隔符字符,將其拆分為單詞,將其分成組,過濾那些具有多於1個元素的組,然后將剩余的組減少到第一個每個元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM