[Haskell-beginners] cascade of if statements

Emmanuel Touzery etouzery at gmail.com
Wed Oct 31 11:25:05 CET 2012


Hello,

 I am wondering if this code can be made a bit nicer?

 I am checking if a file exists... and then if it exists I'm checking
whether it's older than two hours from now. It is a bit annoying to have
that second block in the else (i could make a second function but i think
it all belongs in this function).

 This structure makes me think about the Maybe monad, where it would
automatically exit after it would find out that the file doesn't exists,
instead of having a cascade of if statements inside one another.

 But this function is already in the IO monad (maybe I'm thinking about
this in the wrong way though).

 Any idea for an improvement?

-- don't re-fetch if I fetched in the last 2 hours
needToFetchAgain :: FilePath -> IO Bool
needToFetchAgain export_filename = do
    exists <- doesFileExist export_filename
    if (not exists)
        then return False
        else do
            modif <- getModificationTime export_filename
            curTime <- getClockTime
            let diff = diffClockTimes curTime modif
            return $ tdSec diff >= (3600*2) -- 2 hours

Thank you!

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121031/fc1fd3e8/attachment.htm>


More information about the Beginners mailing list