Early return in IO monad

Hey, Is it somehow possible to return "early" in a do block of the IO monad? The eqivalent do in C: void doBlock() { if (some preCondition) { return; } ... } ??? Thanks! Nathan

On Sat, Aug 31, 2013 at 08:05:26PM +0200, Nathan Hüsken wrote:
Hey,
Is it somehow possible to return "early" in a do block of the IO monad? The eqivalent do in C:
void doBlock() { if (some preCondition) { return; } ... }
???
Nope. But you can say do ... when (not (some condition)) $ do the rest of the stuff You can even indent "the rest of the stuff" directly under the 'when' if you like, though IMO that may be a bit confusing. -Brent

You can use the EitherT monad with IO and use the 'left' function
which is just 'fail' to return early if you aren't restricted to only
use the IO monad.
Sent from my iGNU
On 31. aug. 2013, at 20.05, Nathan Hüsken
Hey,
Is it somehow possible to return "early" in a do block of the IO monad? The eqivalent do in C:
void doBlock() { if (some preCondition) { return; } ... }
???
Thanks! Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

unless somePreCondition ...
On 01/09/2013 4:07 AM, "Nathan Hüsken"
Hey,
Is it somehow possible to return "early" in a do block of the IO monad? The eqivalent do in C:
void doBlock() { if (some preCondition) { return; } ... }
???
Thanks! Nathan
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners

On Sun, Sep 1, 2013 at 1:05 AM, Nathan Hüsken
The eqivalent do in C:
void doBlock() { if (some preCondition) { return; } ... }
Brent and Tony have already given the closest Haskell equivalents. For completeness, it's worth pointing out that the above C is equivalent to void doBlock() { if (some preCondition) { return; } else { ... } } So you can write it similarly in Haskell using if/then/else. -- Kim-Ee
participants (5)
-
Brent Yorgey
-
Kim-Ee Yeoh
-
Nathan Hüsken
-
Tidus Zero
-
Tony Morris