Teacup rim text: Difference between revisions
m
→Haskell Filtering anagrams: Tidied
No edit summary |
m (→Haskell Filtering anagrams: Tidied) |
||
Line 534:
Or taking a different approach, we can avoid the use of Data.Set by obtaining the groups of anagrams (of more than two characters) in the lexicon, and filtering out a circular subset of these:
<lang haskell>import Data.
import Data.List (groupBy, intercalate, sort, sortOn)
import Data.Ord (comparing)
main :: IO ()
main =
readFile "mitWords.txt"
>>= ( putStrLn
. unlines
. fmap (intercalate " -> ")
. (circularOnly =<<)
. anagrams
. lines
)
anagrams :: [String] -> [[String]]
anagrams ws =
groupBy (on (==) fst) (sortOn fst (((,) =<< sort) <$> ws)) >>=▼
| otherwise = []
in groupBy
(on (==) fst)
>>= (go <*> ((> 2) . length))
circularOnly :: [String] -> [[String]]
circularOnly ws
▲ let h = head ws
| otherwise = [h : rs]
rs = filter (isRotation h) (tail ws)▼
where
▲ in bool [h : rs] [] ((length h - 1) > length rs)
h = head ws
isRotation :: String -> String -> Bool
isRotation xs ys =
xs
/= until
( (||)
. (ys ==)
<*> (xs ==)
)
rotated
(rotated xs)
rotated :: [a] -> [a]
rotated [] = []
rotated (x : xs) = xs
{{Out}}
<pre>arc -> rca -> car
|