Alternade words: Difference between revisions

Initial Haskell version.
(J)
(Initial Haskell version.)
Line 1,513:
57: truant -> tun rat
58: twirly -> til wry
</pre>
 
=={{header|Haskell}}==
<lang haskell>import qualified Data.Set as S
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
 
-- Two words constructed from the alternating characters of a single word.
alt :: T.Text -> (T.Text, T.Text)
alt = T.foldl' (\(l, r) c -> (r `T.snoc` c, l)) (T.empty, T.empty)
 
-- The alternades of a list of words.
alternades :: S.Set T.Text -> [T.Text] -> [(T.Text, (T.Text, T.Text))]
alternades dict ws = filter (areMembers . snd) $ zip ws $ map alt ws
where areMembers (x, y) = S.member x dict && S.member y dict
 
main :: IO ()
main = TIO.interact $ \txt ->
let words' = map T.toLower $ T.lines txt
dict = S.fromList words'
in T.unlines $ map alterShow $ alternades dict $ filter longEnough words'
where longEnough = (>= 6) . T.length
alterShow (w, (x, y)) = T.unwords [w, x, y]</lang>
{{out}}
<pre>
$ alternade < unixdict.txt
accost cot acs
accuse cue acs
afield fed ail
agleam gem ala
alcott lot act
allele lee all
allied lid ale
alpert let apr
ambient abet min
annette ante net
apport pot apr
ariadne aide ran
assist sit ass
battle ate btl
blaine lie ban
brenda rna bed
calliope aloe clip
choose hoe cos
choosy hoy cos
claire lie car
collude clue old
effete fee eft
fabric arc fbi
fealty ely fat
fluent let fun
forwent fret own
friend red fin
george ere gog
inroad nod ira
israel sal ire
jaunty any jut
joanne one jan
lounge one lug
oriole roe oil
oswald sad owl
parrot art pro
peoria era poi
pierre ire per
poodle ode pol
pounce one puc
racial ail rca
realty ely rat
sordid odd sri
spatial sail pta
sprain pan sri
strain tan sri
strait tat sri
sturdy try sud
sweaty way set
tattle ate ttl
theorem term hoe
though huh tog
throaty tray hot
triode roe tid
triune rue tin
troupe rue top
truant rat tun
twirly wry til
</pre>
 
Anonymous user