[Haskell-cafe] do vs. pattern matching

Benjamin Edwards edwards.benj at gmail.com
Sat Aug 4 09:10:45 CEST 2012


The do notation in this instance yields a nice advantage: if you want to
switch to a different monad to encapsulate failure you will meely need to
swap out the type signature and your function will need no further work.
On Aug 4, 2012 7:35 AM, "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?
>
> Thanks,
> - Matthew
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120804/1bca28d9/attachment.htm>


More information about the Haskell-Cafe mailing list