Teacup rim text: Difference between revisions
Content added Content deleted
No edit summary |
m (→Haskell Filtering anagrams: Tidied) |
||
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. |
<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 = |
||
⚫ | |||
⚫ | |||
| p = [fmap snd x] |
|||
| otherwise = [] |
|||
in groupBy |
|||
(on (==) fst) |
|||
⚫ | |||
>>= (go <*> ((> 2) . length)) |
|||
circularOnly :: [String] -> [[String]] |
circularOnly :: [String] -> [[String]] |
||
circularOnly ws |
circularOnly ws |
||
⚫ | |||
⚫ | |||
| otherwise = [h : rs] |
|||
⚫ | |||
where |
|||
⚫ | |||
h = head ws |
|||
⚫ | |||
isRotation :: String -> String -> Bool |
isRotation :: String -> String -> Bool |
||
isRotation xs ys = |
isRotation xs ys = |
||
xs |
|||
/= until |
|||
( (||) |
|||
. (ys ==) |
|||
<*> (xs ==) |
|||
) |
|||
rotated |
|||
(rotated xs) |
|||
rotated :: [a] -> [a] |
rotated :: [a] -> [a] |
||
rotated [] = [] |
rotated [] = [] |
||
rotated (x:xs) = xs |
rotated (x : xs) = xs <> [x]</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>arc -> rca -> car |
<pre>arc -> rca -> car |