[Haskell-cafe] do vs. pattern matching

Matthew wonderzombie at gmail.com
Sun Aug 5 02:35:25 CEST 2012

On Sat, Aug 4, 2012 at 7:05 AM, Alexander Solla <alex.solla at gmail.com> wrote:
> On Fri, Aug 3, 2012 at 11:34 PM, Matthew <wonderzombie at gmail.com> wrote:
>> I'm a somewhat experienced coder but I am relatively new to Haskell.
>> I've got a question about whether a usage of do notation is idiomatic,
>> or whether it's better to use pattern matching.
>> I've got two functions which take an input and return Maybe SomeType.
>> If either returns Nothing, I also want to return Nothing. If they both
>> return something, then I'll return something unrelated.
>> With do notation, I can write something like this:
>>         do
>>           foo <- callFoo x
>>           bar <- callBar x
>>           return (baz)
>> Alternatively, there's a straightforward pattern match. After binding
>> foo, bar in a couple of where clauses:
>>         case (foo,bar) of (Just x, Just y) -> baz
>>                           _                -> Nothing
>> So which approach is more idiomatic, do you think?
> The short answer is to write a "one liner" using (>>=) and (>>), unless you
> need to bind more than one value to a variable.  In that case, you should
> use an applicative interface, if available and otherwise possible, and
> finally do-notation.

Aha. I'd forgotten all about >>.

Thanks, everyone! I'm going with callFoo >> callBar >> return baz.

More information about the Haskell-Cafe mailing list