pattern matching on a common element

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

pattern matching on a common element

briand-3
Here's what I'm doing:

data X = A1 String Double | A2 String Int | A3 String Double Int

name c =
  case c of
    A1 name _ -> name
    A2 name _ -> name
    A3 name _ _ -> name

I'm sure there's a better way...
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

Rahul Muttineni
data X =
  A1 { name :: String, d :: Double}
| A2 { name :: String, i :: Int}
| A3 { name :: String, d1 :: Double, i1 :: Int}

Now you can use `name` directly to get the string component of the different variants.

Hope that helps!
Rahul

On Fri, Nov 25, 2016 at 11:16 AM, <[hidden email]> wrote:
Here's what I'm doing:

data X = A1 String Double | A2 String Int | A3 String Double Int

name c =
  case c of
    A1 name _ -> name
    A2 name _ -> name
    A3 name _ _ -> name

I'm sure there's a better way...
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--
Rahul Muttineni

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

briand-3
On Fri, 25 Nov 2016 12:06:06 +0530
Rahul Muttineni <[hidden email]> wrote:

> data X =
>   A1 { name :: String, d :: Double}
> | A2 { name :: String, i :: Int}
> | A3 { name :: String, d1 :: Double, i1 :: Int}
>
> Now you can use `name` directly to get the string component of the
> different variants.
>
> Hope that helps!

oops, i forgot to mention.

i'd like to be able to write my code;

x =  [ A1 "a1" 2.0, A2 "a2" 3 ]

etc... to save myself a lot of typing.


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

Jeffrey Brown
You still can! Using Rahul's solution, that is.

On Thu, Nov 24, 2016 at 11:08 PM, <[hidden email]> wrote:
On Fri, 25 Nov 2016 12:06:06 +0530
Rahul Muttineni <[hidden email]> wrote:

> data X =
>   A1 { name :: String, d :: Double}
> | A2 { name :: String, i :: Int}
> | A3 { name :: String, d1 :: Double, i1 :: Int}
>
> Now you can use `name` directly to get the string component of the
> different variants.
>
> Hope that helps!

oops, i forgot to mention.

i'd like to be able to write my code;

x =  [ A1 "a1" 2.0, A2 "a2" 3 ]

etc... to save myself a lot of typing.


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(I often miss messages here)   |   Github   

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

briand-3
On Thu, 24 Nov 2016 23:09:26 -0800
Jeffrey Brown <[hidden email]> wrote:

> You still can! Using Rahul's solution, that is.

but wouldn't i have to write
 
  A1 {name="a1", d="2.0}
  A2 {name="a2", i=2}

etc...

?

That would be ok for these simple examples, but for my actual code the field names would not be just 1 or 2 characters.


Brian

>
> On Thu, Nov 24, 2016 at 11:08 PM, <[hidden email]> wrote:
>
> > On Fri, 25 Nov 2016 12:06:06 +0530
> > Rahul Muttineni <[hidden email]> wrote:
> >  
> > > data X =
> > >   A1 { name :: String, d :: Double}
> > > | A2 { name :: String, i :: Int}
> > > | A3 { name :: String, d1 :: Double, i1 :: Int}
> > >
> > > Now you can use `name` directly to get the string component of the
> > > different variants.
> > >
> > > Hope that helps!  
> >
> > oops, i forgot to mention.
> >
> > i'd like to be able to write my code;
> >
> > x =  [ A1 "a1" 2.0, A2 "a2" 3 ]
> >
> > etc... to save myself a lot of typing.
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [hidden email]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >  
>
>
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

Rein Henrichs
No, you would not. Record syntax is an addition to, not a replacement for, the form you want to use.

On Thu, Nov 24, 2016 at 11:34 PM <[hidden email]> wrote:
On Thu, 24 Nov 2016 23:09:26 -0800
Jeffrey Brown <[hidden email]> wrote:

> You still can! Using Rahul's solution, that is.

but wouldn't i have to write

  A1 {name="a1", d="2.0}
  A2 {name="a2", i=2}

etc...

?

That would be ok for these simple examples, but for my actual code the field names would not be just 1 or 2 characters.


Brian

>
> On Thu, Nov 24, 2016 at 11:08 PM, <[hidden email]> wrote:
>
> > On Fri, 25 Nov 2016 12:06:06 +0530
> > Rahul Muttineni <[hidden email]> wrote:
> >
> > > data X =
> > >   A1 { name :: String, d :: Double}
> > > | A2 { name :: String, i :: Int}
> > > | A3 { name :: String, d1 :: Double, i1 :: Int}
> > >
> > > Now you can use `name` directly to get the string component of the
> > > different variants.
> > >
> > > Hope that helps!
> >
> > oops, i forgot to mention.
> >
> > i'd like to be able to write my code;
> >
> > x =  [ A1 "a1" 2.0, A2 "a2" 3 ]
> >
> > etc... to save myself a lot of typing.
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [hidden email]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >
>
>
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

Daniel Trstenjak-2
In reply to this post by Rahul Muttineni

Hi Rahul,

On Fri, Nov 25, 2016 at 12:06:06PM +0530, Rahul Muttineni wrote:
> data X =
>   A1 { name :: String, d :: Double}
> | A2 { name :: String, i :: Int}
> | A3 { name :: String, d1 :: Double, i1 :: Int}
>
> Now you can use `name` directly to get the string component of the different
> variants.

It's not recommended to mix record syntax and ADTs, because you
can get runtime errors that the compiler can't catch during compile
time, like calling:

   i (A1 "foo" 3.2)

If you're having the same field in all variants, then an
other approach might be better:

   data A = Ai Int | Ad Double | Aid Int Double

   data X = X { name :: String, a :: A }

Greetings,
Daniel
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

briand-3
In reply to this post by Rein Henrichs
On Fri, 25 Nov 2016 08:41:46 +0000
Rein Henrichs <[hidden email]> wrote:

> No, you would not. Record syntax is an addition to, not a replacement for,
> the form you want to use.
>

oh, I see.  I had tried

 A1 { "a1" 2.0 }

instead of

  A1 ("a1" 2.0)


Brian
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

briand-3
In reply to this post by Daniel Trstenjak-2
On Fri, 25 Nov 2016 10:19:14 +0100
Daniel Trstenjak <[hidden email]> wrote:

> Hi Rahul,
>
> On Fri, Nov 25, 2016 at 12:06:06PM +0530, Rahul Muttineni wrote:
> > data X =
> >   A1 { name :: String, d :: Double}
> > | A2 { name :: String, i :: Int}
> > | A3 { name :: String, d1 :: Double, i1 :: Int}
> >
> > Now you can use `name` directly to get the string component of the different
> > variants.  
>
> It's not recommended to mix record syntax and ADTs, because you
> can get runtime errors that the compiler can't catch during compile
> time, like calling:
>
>    i (A1 "foo" 3.2)
>

agreed.  I'm doing this as a form of shorthand instead of creating a text file based input and parsing it.  Having any sort of type checking is an advantage.

> If you're having the same field in all variants, then an
> other approach might be better:
>
>    data A = Ai Int | Ad Double | Aid Int Double
>
>    data X = X { name :: String, a :: A }
>

yes, that would be better.

interestingly in my journeys through the intertubes I have not found a single mention of using the "()" syntax in place of the "{fieldname=value, ...}" syntax as the generator.


Brian

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

Daniel Trstenjak-2

Hi Brian,

> interestingly in my journeys through the intertubes I have not found a
> single mention of using the "()" syntax in place of the
> "{fieldname=value, ...}" syntax as the generator.

The '()' isn't part of the 'data constructor' (the usual term for A1/A2/A3),
but sometimes needed to help the compiler to make an unambiguous parsing of the expression.

These are all valid expressions:

    i $ A1 "foo" 3.2   -- '$' is often used to get rid of parentheses

    let a1 = A1 "foo" 3.2 in i a1

Greetings,
Daniel
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching on a common element

briand-3
On Fri, 25 Nov 2016 18:18:21 +0100
Daniel Trstenjak <[hidden email]> wrote:

> Hi Brian,
>
> > interestingly in my journeys through the intertubes I have not found a
> > single mention of using the "()" syntax in place of the
> > "{fieldname=value, ...}" syntax as the generator.  
>
> The '()' isn't part of the 'data constructor' (the usual term for A1/A2/A3),
> but sometimes needed to help the compiler to make an unambiguous parsing of the expression.
>
> These are all valid expressions:
>
>     i $ A1 "foo" 3.2   -- '$' is often used to get rid of parentheses
>
>     let a1 = A1 "foo" 3.2 in i a1
>

oh, i see. everything's a function.

thanks!


Brian
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners