How to exit main in haskell subject to conditions

I have a main function that does a lot of I / O. However, at some point I want to check the variable, how not (null shouldBeNull) to exit the entire program without continuing with linux 1 exit code and display an error message.

I tried playing with error "..." , for example, putting it in if :

if (not (null shouldBeNull)) error "something bad happened" else putStrLn "ok"

but I get parse error (possibly incorrect indentation) : (.

Here's the modified snippet.

 main :: IO ExitCode main = do --Get the file name using program argument args <- getArgs file <- readFile (args !! 0) putStrLn("\n") -- ... (some other io) -- [DO A CHECK HERE], exit according to check.. -- ... (even more io) echotry <- system "echo success" rmtry <- system "rm -f test.txt" system "echo done." 

As you can see, I want to check where I added the comment [DO A CHECK HERE] above ...

Thanks for your help!

+6
source share
1 answer

The parsing error is that you are missing the then keyword in the if .

 if condition then truePart else falsePart 

For exit, a more suitable choice than error might be to use one of the functions from System.Exit , for example exitFailure .

So for example

 if not $ null shouldBeNull then do putStrLn "something bad happened" exitFailure else putStrLn "ok" 
+12
source

Source: https://habr.com/ru/post/901905/


All Articles