Matthew | 4 Aug 2012 08:34
Picon

do vs. pattern matching

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
Benjamin Edwards | 4 Aug 2012 09:10
Picon

Re: do vs. pattern matching

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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Johan Holmquist | 4 Aug 2012 11:24
Picon

Re: do vs. pattern matching

Also if you don't need foo and bar you can write:

callFoo >> callBar >> return baz

//Johan

On Aug 4, 2012 8:36 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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Alexander Solla | 4 Aug 2012 16:05
Picon

Re: do vs. pattern matching



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.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Matthew | 5 Aug 2012 02:35
Picon

Re: do vs. pattern matching

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.
Donn Cave | 5 Aug 2012 04:06
Favicon

Re: do vs. pattern matching

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:
...
>> With do notation, I can write something like this:
>>
>>         do
>>           foo <- callFoo x
>>           bar <- callBar x
>>           return (baz)
>>
...
> 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.

But the longer answer would be, it depends!  Right?  The `do' notation
is clear and easy to follow; it's low maintenance - even if you have
nothing to bind right now, if that comes up in the future, it will
drop right into that `do' block;  it's classic Haskell that doesn't
need any explaining to (hardly) anyone.  Maybe it's your last choice,
maybe it's my first.

	Donn

Gmane