Created
September 18, 2008 05:16
-
-
Save nkpart/11377 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Trial where | |
import System.IO | |
import System.Directory | |
import System.Time | |
fileExists :: FilePath -> IO Bool | |
fileExists name = do | |
fileExists <- doesFileExist name | |
if fileExists | |
then return True | |
else doesDirectoryExist name | |
readDay :: String -> Int | |
readDay = read . head . lines | |
readOrCreate :: FilePath -> String -> IO String | |
readOrCreate fp def = do | |
exists <- fileExists fp | |
if exists | |
then readFile fp | |
else writeFile fp def >> return def | |
class TrialMechanism a where | |
daysRemaining :: Int -> a -> IO Int | |
updateDaysRemaining :: a -> Int -> IO () | |
instance TrialMechanism FilePath where | |
daysRemaining def fp = fmap readDay (readOrCreate fp $ show def) | |
updateDaysRemaining fp newDays = writeFile fp $ show newDays | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment