Last active
March 5, 2018 16:10
-
-
Save denisshevchenko/9d682a59f97fae82be58f06941c192f7 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
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