Created
February 11, 2013 15:21
-
-
Save robinp/4755081 to your computer and use it in GitHub Desktop.
Playing around with flattening nested if-then-else
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import System.FilePath | |
import System.Directory | |
main = firstWhereM check ["a", "x", "b"] >>= print | |
where check = doesFileExist . (</>"z"</>"y") | |
firstWhereM :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a) | |
firstWhereM pred ins = case ins of | |
[] -> return Nothing | |
a:as -> pred a >>= \b -> | |
if b then return (Just a) | |
else firstWhereM pred as |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment