Find words which contains more than 3 e vowels: Difference between revisions

→‎{{header|Haskell}}: Added a variant, defining the predicate in terms of a single fold
(→‎{{header|Haskell}}: Added a variant, defining the predicate in terms of a single fold)
Line 1,176:
valid w = not (any (`elem` "aiou") w) && length (filter (=='e') w) > 3</syntaxhighlight>
 
Or, defining the predicate in terms of a single fold:
{{out}}
 
<syntaxhighlight lang="haskell">import System.IO (readFile)
import Data.Bifunctor (first)
 
 
-------- MORE THAN THREE VOWELS, AND NONE BUT E -------
 
p :: String -> Bool
p = uncurry (&&) . (first (3 <) . foldr rule (0, True))
rule :: Char -> (Int, Bool) -> (Int, Bool)
rule _ (n, False) = (n, False)
rule 'e' (n, b) = (succ n, b)
rule c (n, b)
| c `elem` "aiou" = (n, False)
| otherwise = (n, b)
 
--------------------------- TEST -------------------------
main :: IO ()
main = readFile "unixdict.txt"
>>= (putStrLn . unlines . filter p . lines)</syntaxhighlight>
{{out}}
<pre>belvedere
dereference
9,655

edits