[Haskell-beginners] Proper use of exit{Success,Failure}

Alex Hammel ahammel87 at gmail.com
Tue Jun 9 15:38:15 UTC 2015


exitSuccess :: IO a means that exitSuccess type-checks for any `a`. Your
program should typecheck without `void`. I suspect you've got a stray paren.

This prints "Bailing out." and exits zero for me:

module Main where

import System.Exit (exitSuccess)
import Control.Monad (when)

main = do
    let condition = True
    when condition (putStrLn "Bailing out." >> exitSuccess)
    putStrLn "Continuing normally..."


On Tue, 9 Jun 2015 at 08:27 Sumit Sahrawat, Maths & Computing, IIT (BHU) <
sumit.sahrawat.apm13 at iitbhu.ac.in> wrote:

> Try `Control.Monad.void`. It is equivalent of adding `>> return ()` to the
> end of a block.
>
> void $ do
>  ...
>
> On 9 June 2015 at 20:55, Vale Cofer-Shabica <vale.cofershabica at gmail.com>
> wrote:
>
>> Dear all,
>>
>> I'm writing a command line program which may need to exit early based
>> on some condition. Because of the pipeline the program is a part of, I
>> want to be able to control the exit status when it does terminate. I
>> can successfully use 'error' to indicate failure, but also want to
>> exit early with success (or any other status). Here's what I've
>> managed:
>>
>> >import System.Exit (exitSuccess)
>> >import Control.Monad (when)
>>
>> >main = do
>>
>> ... code to reasonably initialize condition, e.g.:
>>
>> >  let condition = True
>> >  when condition (putStrLn "Bailing out.")>>exitSuccess>>(return ())
>>
>> ... program continues
>>
>> If I remove ">>(return ())" my code will not typecheck because
>> "exitSuccess :: IO a". But this looks ugly and smells sufficiently
>> like a kludge that I presume there is a better way to do it.
>>
>> Any suggestions appreciated,
>> vale
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
>
> --
> Regards
>
> Sumit Sahrawat
>  _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150609/271d0780/attachment.html>


More information about the Beginners mailing list