Teacup rim text: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
→‎{{header|Haskell}}: Switched from unix to MIT dictionary, formatted output
Line 114: Line 114:
=={{header|Haskell}}==
=={{header|Haskell}}==
Circular words of more than 2 characters in a local copy of unixdict.txt
Circular words of more than 2 characters in a local copy of unixdict.txt
<lang haskell>import Control.Applicative (liftA2)
<lang haskell>import Data.List (groupBy, intercalate, sort, sortBy)
import Control.Applicative (liftA2)
import qualified Data.Set as S
import qualified Data.Set as S
import Data.Ord (comparing)
import Data.Function (on)



main :: IO ()
main :: IO ()
main =
main = readFile "unixdict.txt" >>= (print . circularWords . lines)
readFile "mitWords.txt" >>= (putStrLn . showGroups . circularWords . lines)


circularWords :: [String] -> [String]
circularWords :: [String] -> [String]
Line 126: Line 131:


isCircular :: S.Set String -> String -> Bool
isCircular :: S.Set String -> String -> Bool
isCircular lex w =
isCircular lex w = 2 < length w && all (`S.member` lex) (rotations w)

2 < length w && all (`S.member` lex) (rotations w)
rotations :: [a] -> [[a]]
rotations :: [a] -> [[a]]
rotations = liftA2 fmap rotated (enumFromTo 0 . pred . length)
rotations = liftA2 fmap rotated (enumFromTo 0 . pred . length)
Line 134: Line 138:
rotated :: [a] -> Int -> [a]
rotated :: [a] -> Int -> [a]
rotated [] _ = []
rotated [] _ = []
rotated xs n = zipWith const (drop n (cycle xs)) xs</lang>
rotated xs n = zipWith const (drop n (cycle xs)) xs

showGroups :: [String] -> String
showGroups xs =
unlines $
intercalate " -> " . fmap snd <$>
groupBy (on (==) fst) (sortBy (comparing fst) (((,) =<< sort) <$> xs))</lang>
{{Out}}
{{Out}}
<pre>aaa
<pre>["aaa","apt","arc","ate","car","eat","iii","pta","rca","tap","tea"]</pre>
arc -> car -> rca
ate -> eat -> tea
aim -> ima -> mai
asp -> pas -> spa
iii
ips -> psi -> sip
ooo
www
xxx</pre>


=={{header|JavaScript}}==
=={{header|JavaScript}}==