[Haskell-cafe] How to make boolean logic with IO Monad more expressive?
Emil Axelsson
78emil at gmail.com
Mon May 27 07:35:04 UTC 2019
Not with the standard `||` and `&&` operations. You'd have to define new
versions with a `Monad` constraint.
The `monad-loops` package is pretty nice for short-circuiting stuff.
See, for example, `andM` and `orM`. But it doesn't provide any binary
operators.
https://hackage.haskell.org/package/monad-loops
/ Emil
Den 2019-05-27 kl. 07:07, skrev Magicloud Magiclouds:
> Hi,
>
> I think `a || b && c` is more clear than
> ```
> if a
> then True
> else if b
> then if c
> then True
> else False
> else False
> ```
>
> And some languages, `pureComputing || (ioOperation && pure2)` involves
> IO only when pureComputing is False.
>
> So in Haskell, is it possible to get both benefits?
> ```
> status <- ioOperation -- IO-ed anyway
> return $ pureComputing || (status && pure2)
> ```
> ```
> if pureComputing
> then return True
> else do
> status <- ioOperation
> if status
> then return pure2
> else return False
> -- Looks ugly
> ```
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe
mailing list