Find words which contains all the vowels: Difference between revisions

m
→‎{{header|Haskell}}: filter rather than partition (simpler here)
(→‎{{header|Haskell}}: Added a variant, expressed in terms of Data.Set, which includes the restriction to 10 chars)
m (→‎{{header|Haskell}}: filter rather than partition (simpler here))
Line 1,269:
or, adding the restriction to words of more than 10 characters, and using Data.Set to identify words with a set of 5 unique vowel characters, which have no more than 5 vowel characters in total:
 
<syntaxhighlight lang="haskell">import Data.SetList (Set, fromList, member, sizepartition)
import Data.ListSet (partitionSet, fromList, member, size)
import Data.Bifunctor (bimap)
import Control.Monad (join)
 
---- WORDS OVER 10 CHARS WHICH CONTAIN EACH VOWEL ONCE ---
Line 1,280 ⟶ 1,278:
 
eachVowelOnce :: String -> Bool
eachVowelOnce w =
(uncurry (&&) . both (== 5)) $
((,) . length <*> (size . fromList)) $
both (5 ==)
(partitionfilter (`member` vowels) w)
(bimap
(size . fromList)
((-) (length w) . length)
(partition (`member` vowels) w)
)
 
vowels :: Set Char
vowels = fromList "aeiou"
 
--------------------------- TEST -------------------------
main :: IO ()
main =
readFile "unixdict.txt"
>>= (mapM_ putStrLn . filter p . lines)
)
 
 
------------------------- GENERIC ------------------------
 
both :: (a -> b) -> (a, a) -> (b, b)
both f ~(x, y) = join(f x, f bimapy)</syntaxhighlight>
{{Out}}
<pre>ambidextrous
9,659

edits