Guest User

Untitled

a guest
Feb 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. module Trial where
  2.  
  3. import System.IO
  4. import System.Directory
  5. import System.Time
  6.  
  7. fileExists :: FilePath -> IO Bool
  8. fileExists name = do
  9. fileExists <- doesFileExist name
  10. if fileExists
  11. then return True
  12. else doesDirectoryExist name
  13.  
  14. readDay :: String -> Int
  15. readDay = read . head . lines
  16.  
  17. readOrCreate :: FilePath -> String -> IO String
  18. readOrCreate fp def = do
  19. exists <- fileExists fp
  20. if exists
  21. then readFile fp
  22. else writeFile fp def >> return def
  23.  
  24. class TrialMechanism a where
  25. daysRemaining :: Int -> a -> IO Int
  26. updateDaysRemaining :: a -> Int -> IO ()
  27.  
  28. instance TrialMechanism FilePath where
  29. daysRemaining def fp = fmap readDay (readOrCreate fp $ show def)
  30. updateDaysRemaining fp newDays = writeFile fp $ show newDays
Add Comment
Please, Sign In to add comment