Find words which contains all the vowels: Difference between revisions

Content added Content deleted
(→‎{{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: 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:
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.Set (Set, fromList, member, size)
<syntaxhighlight lang="haskell">import Data.List (partition)
import Data.List (partition)
import Data.Set (Set, fromList, member, size)
import Data.Bifunctor (bimap)
import Control.Monad (join)


---- WORDS OVER 10 CHARS WHICH CONTAIN EACH VOWEL ONCE ---
---- WORDS OVER 10 CHARS WHICH CONTAIN EACH VOWEL ONCE ---
Line 1,280: Line 1,278:


eachVowelOnce :: String -> Bool
eachVowelOnce :: String -> Bool
eachVowelOnce w =
eachVowelOnce w =
uncurry (&&) $
(uncurry (&&) . both (== 5)) $
((,) . length <*> (size . fromList)) $
both (5 ==)
filter (`member` vowels) w
(bimap
(size . fromList)
((-) (length w) . length)
(partition (`member` vowels) w)
)


vowels :: Set Char
vowels :: Set Char
vowels = fromList "aeiou"
vowels = fromList "aeiou"


--------------------------- TEST -------------------------
--------------------------- TEST -------------------------
main :: IO ()
main :: IO ()
main =
main =
readFile "unixdict.txt"
readFile "unixdict.txt"
>>= (mapM_ putStrLn . filter p . lines)
>>= (mapM_ putStrLn . filter p . lines)



------------------------- GENERIC ------------------------
------------------------- GENERIC ------------------------


both :: (a -> b) -> (a, a) -> (b, b)
both :: (a -> b) -> (a, a) -> (b, b)
both = join bimap</syntaxhighlight>
both f ~(x, y) = (f x, f y)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>ambidextrous
<pre>ambidextrous