Skip to content

Instantly share code, notes, and snippets.

@robinp
Created February 11, 2013 15:21
Show Gist options
  • Save robinp/4755081 to your computer and use it in GitHub Desktop.
Save robinp/4755081 to your computer and use it in GitHub Desktop.
Playing around with flattening nested if-then-else
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