
In my code, I'm doing this quite a lot: x <- someIO case x of Opt1 -> ... Having a line for extracting the value from the IO (or STM) and then acting on the value seems unnatural. Is there a more concise way to do this? This code: case someIO of Opt1 -> ... Doesn't work, but is there something like that, that is valid?

See this thread:
http://www.haskell.org/pipermail/haskell-cafe/2010-October/084291.html
On 14 March 2011 15:48, tsuraan
In my code, I'm doing this quite a lot:
x <- someIO case x of Opt1 -> ...
Having a line for extracting the value from the IO (or STM) and then acting on the value seems unnatural. Is there a more concise way to do this? This code:
case someIO of Opt1 -> ...
Doesn't work, but is there something like that, that is valid?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ozgur Akgun

See this thread: http://www.haskell.org/pipermail/haskell-cafe/2010-October/084291.html
Which links to http://hackage.haskell.org/trac/ghc/ticket/4359 . Looks like there's already been quite a bit of discussion on this already :) Thanks for the link.

Your first line is entirely natural. The alternative doesn't look right at all. I am not aware of a more concise alternative to this general construction (assuming there are multiple case alternative, and that the work can't be done with library functions). Chris From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of tsuraan Sent: 14 March 2011 15:49 To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Question on a common pattern In my code, I'm doing this quite a lot: x <- someIO case x of Opt1 -> ... Having a line for extracting the value from the IO (or STM) and then acting on the value seems unnatural. Is there a more concise way to do this? This code: case someIO of Opt1 -> ... Doesn't work, but is there something like that, that is valid? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe _____ No virus found in this message. Checked by AVG - www.avg.com Version: 10.0.1204 / Virus Database: 1498/3506 - Release Date: 03/14/11

If you have only one alternative, then you can simply do:
Opt1 <- someIO
E.g., if you are _sure_ that foo returns always a 'Just' within a monad you
can perfectly do :
Just x <- foo
2011/3/14 tsuraan
In my code, I'm doing this quite a lot:
x <- someIO case x of Opt1 -> ...
Having a line for extracting the value from the IO (or STM) and then acting on the value seems unnatural. Is there a more concise way to do this? This code:
case someIO of Opt1 -> ...
Doesn't work, but is there something like that, that is valid?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

If you have only one alternative, then you can simply do:
Opt1 <- someIO
E.g., if you are _sure_ that foo returns always a 'Just' within a monad you can perfectly do :
Just x <- foo
That's interesting. I had no idea that one could do that. I think what I'm looking for is something along the lines of lambda-case, but thanks for the tip.

On Mon, 2011-03-14 at 17:56 +0100, Yves Parès wrote:
If you have only one alternative, then you can simply do:
Opt1 <- someIO
E.g., if you are _sure_ that foo returns always a 'Just' within a monad you can perfectly do :
Just x <- foo
Please beware - it is not exactly the same as with case if your assumption is wrong:
(do Just x <- return Nothing; [x]) [] (do x <- return Nothing; case x of Just x -> [x]) *** Exception: <interactive>:1:26-48: Non-exhaustive patterns in case
For IO and many monads it is the same but it is not necessary for any monad. Regards

tsuraan
Is there a more concise way to do this?
I use someIO >>= f where f Opt1 = ... If it's a common pattern, you can even do opts f _ _ (Opt1 x) = f x opts _ g _ (Opt2 x) = g x opts _ _ h (Opt3 x) = h x . Functions are easier to mess around with than case expressions. -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

Quoth Achim Schneider
I use
someIO >>= f where f Opt1 = ...
If it's a common pattern, you can even do
opts f _ _ (Opt1 x) = f x opts _ g _ (Opt2 x) = g x opts _ _ h (Opt3 x) = h x
. Functions are easier to mess around with than case expressions.
I like this ... or, I would like it, if I could make it work! I get "The last statement in a 'do' construct must be an expression", if I don't drag the `where' clause down to the end of the `do' block around `someIO', which of course is what we're trying to avoid with the case expression. I must have missed a trick with the layout? Donn Cave, donn@avvanta.com

Hi, Donn Cave wrote:
someIO>>= f where f Opt1 = ...
I like this ... or, I would like it, if I could make it work!
I get "The last statement in a 'do' construct must be an expression",
Where-clauses can only be used on equations, not on expressions or statements, so you would need to float the where clause outwards: foo = do someIO >>= f where f = ... Or you can use let-in-expressions or let-statements to bind local values (or functions) in do-notation: do let f = ... result <- someIO >>= f or do result <- let f = ... in someIO >>= Op1 = .. HTH, Tillmann

Quoth Tillmann Rendel
Where-clauses can only be used on equations, not on expressions or statements, so you would need to float the where clause outwards:
So ... not to put too fine a point on it, but ... as useful as function notation could be for the present purposes, you seem to agree that because of syntax limits, "where" isn't an answer to the problem. Recalling that we were talking about a common pattern v <- fx case v of ... ... (where v never appears again) Where a Haskell programmer's reflex seems to call for fx >>= case ..., or from the old thread mentioned earlier, a lambda with multiple definitions - fx >>= \ Opt1 -> ... Opt2 -> ... Donn Cave, donn@avvanta.com

Hello tsuraan,
Most often, when we multi-pattern-match on the return value of a monadic
computation, we talk about Maybe or Either or [], and I often find
myself doing this:
someIO1 :: IO (Maybe A)
someIO2 :: IO (Either A B)
result1 <- someIO1 >>= maybe ...
result2 <- someIO2 >>= either ...
There are many ways of encoding this more nicely. My personal way is to
use the proper monad transformers for the purpose. For many of these
situations I have written convenient combinators in the 'contstuff'
package. I found especially the 'liftF' function very useful in these
cases:
liftF :: (LiftFunctor t, Monad m) => m (InnerFunctor t a) -> t m a
Example instances:
liftF :: Monad m => m [a] -> ChoiceT r i m a
liftF :: Monad m => m (Either e a) -> EitherT r e m a
liftF :: Monad m => m (Maybe a) -> MaybeT r m a
That way instead of checking each return value individually you would
just write:
result <- evalMaybeT $ do
x <- liftF someMaybeIO
y <- liftF (someOtherMaybeIO x x)
return (Result x y)
Greets,
Ertugrul
tsuraan
In my code, I'm doing this quite a lot:
x <- someIO case x of Opt1 -> ...
Having a line for extracting the value from the IO (or STM) and then acting on the value seems unnatural. Is there a more concise way to do this? This code:
case someIO of Opt1 -> ...
Doesn't work, but is there something like that, that is valid?
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
participants (9)
-
Achim Schneider
-
Chris Dornan
-
Donn Cave
-
Ertugrul Soeylemez
-
Maciej Marcin Piechotka
-
Ozgur Akgun
-
Tillmann Rendel
-
tsuraan
-
Yves Parès