[Haskell-beginners] cascade of if statements
etouzery at gmail.com
Wed Oct 31 11:27:34 CET 2012
hmm otherwise the return False must be a return True, stupid bug:
-- 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 True
modif <- getModificationTime export_filename
curTime <- getClockTime
let diff = diffClockTimes curTime modif
return $ tdSec diff >= (3600*2) -- 2 hours
On Wed, Oct 31, 2012 at 11:25 AM, Emmanuel Touzery <etouzery at gmail.com>wrote:
> 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!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners