Teacup rim text: Difference between revisions

m
No edit summary
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.ListFunction (groupBy, intercalate, sort, sortOnon)
import Data.List (groupBy, intercalate, sort, sortOn)
import Data.Ord (comparing)
import Data.Function (on)
import Data.Bool (bool)
 
main :: IO ()
main =
readFile "mitWords.txt" >>=
>>= ( putStrLn
(putStrLn .
. unlines
unlines . fmap (intercalate " -> ") . (circularOnly =<<) . anagrams . lines)
. fmap (intercalate " -> ")
. (circularOnly =<<)
. anagrams
. lines
)
 
anagrams :: [String] -> [[String]]
anagrams ws =
let hgo =x head wsp
groupBy (on (==) fst) (sortOn fst (((,) =<< sort) <$> ws)) >>=
(bool [] . return . fmap snd)| <*>p ((>= 2)[fmap .snd length)x]
| otherwise = []
in groupBy
(on (==) fst)
groupBy (on (==) fst) (sortOn fst (((,) =<< sort) <$> ws)) >>=
>>= (go <*> ((> 2) . length))
 
circularOnly :: [String] -> [[String]]
circularOnly ws =
in| bool [h : rs] [] ((length h - 1) > length rs) = []
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
rs = filter (isRotation h) (tail ws)
 
isRotation :: String -> String -> Bool
isRotation xs ys = xs /= until ((||) . (ys ==) <*> (xs ==)) rotated (rotated xs)
xs
/= until
( (||)
. (ys ==)
<*> (xs ==)
)
rotated
(rotated xs)
 
rotated :: [a] -> [a]
rotated [] = []
rotated (x : xs) = xs ++<> [x]</lang>
{{Out}}
<pre>arc -> rca -> car
9,655

edits