Teacup rim text: Difference between revisions

Content added Content deleted
No edit summary
Line 534: 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:
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.List (groupBy, intercalate, sort, sortOn)
<lang haskell>import Data.Function (on)
import Data.List (groupBy, intercalate, sort, sortOn)
import Data.Ord (comparing)
import Data.Ord (comparing)
import Data.Function (on)
import Data.Bool (bool)


main :: IO ()
main :: IO ()
main =
main =
readFile "mitWords.txt" >>=
readFile "mitWords.txt"
>>= ( putStrLn
(putStrLn .
. unlines
unlines . fmap (intercalate " -> ") . (circularOnly =<<) . anagrams . lines)
. fmap (intercalate " -> ")
. (circularOnly =<<)
. anagrams
. lines
)


anagrams :: [String] -> [[String]]
anagrams :: [String] -> [[String]]
anagrams ws =
anagrams ws =
let go x p
groupBy (on (==) fst) (sortOn fst (((,) =<< sort) <$> ws)) >>=
(bool [] . return . fmap snd) <*> ((> 2) . length)
| p = [fmap snd x]
| otherwise = []
in groupBy
(on (==) fst)
(sortOn fst (((,) =<< sort) <$> ws))
>>= (go <*> ((> 2) . length))


circularOnly :: [String] -> [[String]]
circularOnly :: [String] -> [[String]]
circularOnly ws =
circularOnly ws
| (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 :: String -> String -> Bool
isRotation xs ys = xs /= until ((||) . (ys ==) <*> (xs ==)) rotated (rotated xs)
isRotation xs ys =
xs
/= until
( (||)
. (ys ==)
<*> (xs ==)
)
rotated
(rotated xs)


rotated :: [a] -> [a]
rotated :: [a] -> [a]
rotated [] = []
rotated [] = []
rotated (x:xs) = xs ++ [x]</lang>
rotated (x : xs) = xs <> [x]</lang>
{{Out}}
{{Out}}
<pre>arc -> rca -> car
<pre>arc -> rca -> car