Show a list of words repeated in haskell

I need to write a function that displays duplicate words from a string and return a list of strings in the order in which it appears and ignore non-letters

e.g. hugs invitation

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"] 
+4
source share
4 answers

To break a string into words, use the words function (in foreplay). To eliminate characters other than words, filter with Data.Char.isAlphaNum . Fill the list with your tail to get adjacent pairs (x, y) . Add a list containing a new list containing all x where x == y .

Socket:

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

I'm not sure what works, but it should give you a rough idea.

+8
source

I am new to this language, so my decision may be something ugly in the eyes of a Haskell veteran, but anyway:

 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))))) 

In this part, all letters and spaces from the string s will be deleted:

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

This will split the string s into words and group the same words with lists that return a list of lists:

 List.group (words s) 

When this part will delete all lists with less than two elements:

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

After which we will combine all the lists, removing one element from them, although

 concat (map tail s) 
+2
source

It may be inelegant, but it is conceptually very simple. I assume that he is looking for consecutive repeated words like examples.

 -- 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 
0
source

Based on what Alexander Prokofiev answered:

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)))))

Remove unnecessary parentheses:

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)))))

Use $ to remove more brackets (each $ can replace the opening bracket if the trailing bracket is at the end of the expression):

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

Replace character ranges with functions from Data.Char, merge concat, and map:

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

Use a section and sparkle in a dotless style to simplify (\x -> length x > 1) to ((>1) . length) . This combines length with (> 1) (a partially applicable statement or section ) in the loop from right to left.

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

Eliminate the explicit variable "x" to make a full expression without dots:

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

Now the whole function, reading from right to left, is a pipeline that filters only alpha or separator characters, splits it into words, splits it into groups, filters these groups with more than one element, and then reduces the remaining groups to the first element of each of them .

0
source

All Articles