Skip to content

Instantly share code, notes, and snippets.

@denisshevchenko
Last active March 5, 2018 16:10
Show Gist options
  • Save denisshevchenko/9d682a59f97fae82be58f06941c192f7 to your computer and use it in GitHub Desktop.
Save denisshevchenko/9d682a59f97fae82be58f06941c192f7 to your computer and use it in GitHub Desktop.
import System.Directory
import System.Environment
import Control.Exception
import Data.Typeable
import Data.List
data InvalidPath = InvalidPath String deriving (Show)
data FileError = NoSuchFile deriving (Show, Typeable)
data EmptyFile = EmptyFile deriving (Show, Typeable)
instance Exception InvalidPath
instance Exception EmptyFile
readMyFile :: FilePath -> IO String
readMyFile pathToFile = do
fileIsHere <- doesFileExist pathToFile
if fileIsHere then do
content <- readFile pathToFile
case content of
[] -> throw EmptyFile
_ -> return content
else
throw $ InvalidPath "Alert: You entered an incorrect path"
main :: IO ()
main = do
putStrLn "Введите путь к фалу"
content <- getLine
file <- try $ readMyFile content :: IO (Either InvalidPath String)
case file of
Left exc -> print $ show exc
Right content -> putStrLn content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment