Skip to content

Instantly share code, notes, and snippets.

@nkpart
Created September 18, 2008 05:16
Show Gist options
  • Save nkpart/11377 to your computer and use it in GitHub Desktop.
Save nkpart/11377 to your computer and use it in GitHub Desktop.
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