<br><br><div class="gmail_quote">On Fri, Aug 3, 2012 at 11:34 PM, Matthew <span dir="ltr"><<a href="mailto:wonderzombie@gmail.com" target="_blank">wonderzombie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm a somewhat experienced coder but I am relatively new to Haskell.<br>
I've got a question about whether a usage of do notation is idiomatic,<br>
or whether it's better to use pattern matching.<br>
<br>
I've got two functions which take an input and return Maybe SomeType.<br>
If either returns Nothing, I also want to return Nothing. If they both<br>
return something, then I'll return something unrelated.<br>
<br>
With do notation, I can write something like this:<br>
<br>
do<br>
foo <- callFoo x<br>
bar <- callBar x<br>
return (baz)<br>
<br>
Alternatively, there's a straightforward pattern match. After binding<br>
foo, bar in a couple of where clauses:<br>
<br>
case (foo,bar) of (Just x, Just y) -> baz<br>
_ -> Nothing<br>
<br>
So which approach is more idiomatic, do you think?<br></blockquote><div><br></div><div>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.</div>
</div>