import Data.Maybe bruteforce :: (a -> Bool) -> [a] -> Maybe a bruteforce f xs | null result = Nothing | otherwise = Just $ head result where result = mapMaybe bruteforce' xs -- test one instance bruteforce' x | f x = Just x | otherwise = Nothing generatorString :: Int -> [String] generatorString 0 = [""] generatorString deep = concatMap (\x -> map (\ys -> (x:ys)) nextgen) ['a'..'z'] where nextgen = generatorString (deep - 1) main :: IO () main = do putStrLn $ fromJust $ bruteforce ((==) "zabcde") (generatorString 6)