First of all, I'm a novice in Haskell, so if I'm asking somewhat silly question please tell me how can I do better. Thanks :)
My task is to get a list of strings, conforming to specific condition. If no strings conform, I'd like to evaluate to Nothing. So I write two functions:
isSpecialLine :: String -> String -> Maybe Bool
isSpecialLine t s = Just $ (("[" ++ t ++ ":") `isPrefixOf` s) && ("]::" `isSuffixOf` s)
getLinesWith :: String -> String -> Maybe [String]
getLinesWith t = filterM (isSpecialLine t) . lines
This code works, but I find that Maybe Bool looks a bit weird. Hey, it's binary! It's always True or False, so value of isSpecialLine will always be Just True or Just False. It can never be Nothing in my case!
But if I change isSpecialLine output type to Bool, following problem appears: filterM expects Maybe Bool instead of Bool.
Ok, so I do that:
getLinesWith :: String -> String -> Maybe [String]
getLinesWith t = filterM (Just $ isSpecialLine t) . lines
Now compiler complains about unmatching type: Maybe (String -> Bool) doesn't match expected String -> Maybe Bool. Ok, pretty reasonable. So I:
getLinesWith :: String -> String -> Maybe [String]
getLinesWith t = Just $ filter (isSpecialLine t) . lines
And again type mismatching, this time Maybe (String -> [String]) is not String -> Maybe [String]. What's the correct syntax to wrap [String] to Maybe monad?