
Hi, What is right way to do conditional IO? For example, I need to write to file errors only in case they exist, otherwise my function should do nothing: handleParseErrors errors | (not . null) errors = writeFile "parse-errors.txt" (show errors) | otherwise = ? What should be an 'otherwise' case ? Thanks!

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 20/06/11 18:00, Dmitri O.Kondratiev wrote:
Hi, What is right way to do conditional IO? For example, I need to write to file errors only in case they exist, otherwise my function should do nothing:
handleParseErrors errors | (not . null) errors = writeFile "parse-errors.txt" (show errors) | otherwise = ?
What should be an 'otherwise' case ?
How about `return ()'? -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQIcBAEBAgAGBQJN/v/hAAoJEDiWqExGnQ/Q7VQP/js4XZc92bHeM8f/ErQeQ4LT UwINtt8WFtTWevQtwfO6wNc96CVcjnybb2SwPqnQMISnb4EPc9RvfFz24FTBneQ9 d7MD44uNnW1wAgY8VKaQQJ2v5VVrUd8Gq/m0toUKuefaT9yKMVIG/EeEPaLEUe3y tJUyykCXcUpTzllDyk9kjb/1Og9CNWD+y4rdrWSldWCwgIPMHE2nwNOvs6RquPDQ Om5eFdk2AFwAgDl1FIR2EiuWdQ3jUVPaDsHC0L24fvCeC/TA6AoHmC0panCYCchc 8nEruqwmiA/nU+Uh1Z6SWEicBeuWcXSk6iPuRYM7xU9dDLKFNEHAvCTLYdg2+zwR sovOOYrOEnQ8FDwyZGXC7atxrNGl/nk09Q2RNkXryGo/nNdkUG0AuvoXI5gn8Q6v 7mZiXJr2AbUFqcDA4qXOj8IwddtU5hvwqkGj7xSqCck9sHWWXjZLrJqFH+mW8oMU 8rHVkSpMEFH0vbfvvgRMqzdcpnuDKa4/t+o8wvJy/lr9pdhP2o2azpxvb6NFlDpP dgDwnOe/v8c9Mz7tqs41k94SYTVk2lNpsoXShVoB9tg0EttHKjDNM2ykGJl90Hcf 6vJUGEyNqE78s6qMxiAauxatlQdkw40j2ifcriElj5U7cogpZUGxnb/RODsl0rqn BHPJVkHC/iEQ+t/4lfaT =HGbC -----END PGP SIGNATURE-----

| otherwise = return ()
or:
handleParseErrors errors = when (not . null $ errors) $ writeFile .....
2011/6/20 Dmitri O.Kondratiev
Hi, What is right way to do conditional IO? For example, I need to write to file errors only in case they exist, otherwise my function should do nothing:
handleParseErrors errors | (not . null) errors = writeFile "parse-errors.txt" (show errors) | otherwise = ?
What should be an 'otherwise' case ?
Thanks!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Principal Engineer, Mirantis Inc. http://www.mirantis.com/ Editor, http://fprog.ru/

Your errors branch has the type
writeFile "parse-errors.txt" (show errors) :: IO ()
This means that your otherwise branch should have the same type.
You can use the return function that has the type
return :: Monad m => a -> m a
specialised to m = IO
in conjunction with the value
() :: ()
giving
return () :: IO ()
There is also the when function that eliminates the else case for
conditional IO:
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Mon...
On Mon, Jun 20, 2011 at 4:00 PM, Dmitri O.Kondratiev
Hi, What is right way to do conditional IO? For example, I need to write to file errors only in case they exist, otherwise my function should do nothing:
handleParseErrors errors | (not . null) errors = writeFile "parse-errors.txt" (show errors) | otherwise = ?
What should be an 'otherwise' case ?
Thanks!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks! Everything works, and 'when' is really nice.
( I still have only basic monad knowledge, need more time to spend on
existing libraries)
On Mon, Jun 20, 2011 at 12:14 PM, Lyndon Maydwell
Your errors branch has the type
writeFile "parse-errors.txt" (show errors) :: IO ()
This means that your otherwise branch should have the same type.
You can use the return function that has the type
return :: Monad m => a -> m a
specialised to m = IO
in conjunction with the value
() :: ()
giving
return () :: IO ()
There is also the when function that eliminates the else case for conditional IO:
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Mon...
On Mon, Jun 20, 2011 at 4:00 PM, Dmitri O.Kondratiev
wrote: Hi, What is right way to do conditional IO? For example, I need to write to file errors only in case they exist, otherwise my function should do nothing:
handleParseErrors errors | (not . null) errors = writeFile "parse-errors.txt" (show errors) | otherwise = ?
What should be an 'otherwise' case ?
Thanks!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, 20 Jun 2011 10:33:14 +0200, Dmitri O.Kondratiev
Thanks! Everything works, and 'when' is really nice. ( I still have only basic monad knowledge, need more time to spend on existing libraries)
See "A tour of the Haskell Monad functions"[0]. Regards, Henk-Jan van Tuyl [0] http://members.chello.nl/hjgtuyl/tourdemonad.html -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

On Mon, Jun 20, 2011 at 1:33 AM, Dmitri O.Kondratiev
Thanks! Everything works, and 'when' is really nice. ( I still have only basic monad knowledge, need more time to spend on existing libraries)
I heavily use 'when' and 'unless'. ('unless b x' is the same as 'when (not b) x'.)
participants (6)
-
Arlen Cuss
-
David Barbour
-
Dmitri O.Kondratiev
-
Eugene Kirpichov
-
Henk-Jan van Tuyl
-
Lyndon Maydwell