Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  pattern matching on a common element (bri...@aracnet.com)
   2. Re:  pattern matching on a common element (Rahul Muttineni)
   3. Re:  pattern matching on a common element (bri...@aracnet.com)
   4. Re:  pattern matching on a common element (Jeffrey Brown)
   5. Re:  pattern matching on a common element (bri...@aracnet.com)
   6. Re:  pattern matching on a common element (Rein Henrichs)
   7. Re:  pattern matching on a common element (Daniel Trstenjak)


----------------------------------------------------------------------

Message: 1
Date: Thu, 24 Nov 2016 21:46:13 -0800
From: <bri...@aracnet.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161124214613.576a0...@basalt.deldotd.com>
Content-Type: text/plain; charset=US-ASCII

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...


------------------------------

Message: 2
Date: Fri, 25 Nov 2016 12:06:06 +0530
From: Rahul Muttineni <rahulm...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID:
        <canij+er5lv9fn_3pdu_qbbrbf5equoraoh7+u0_zjgn1mxj...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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, <bri...@aracnet.com> 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
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Rahul Muttineni
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20161125/7b67a8b6/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 24 Nov 2016 23:08:02 -0800
From: <bri...@aracnet.com>
To: Rahul Muttineni <rahulm...@gmail.com>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161124230802.2ec92...@basalt.deldotd.com>
Content-Type: text/plain; charset=US-ASCII

On Fri, 25 Nov 2016 12:06:06 +0530
Rahul Muttineni <rahulm...@gmail.com> 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.




------------------------------

Message: 4
Date: Thu, 24 Nov 2016 23:09:26 -0800
From: Jeffrey Brown <jeffbrown....@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID:
        <caec4ma3+qdgbuwsuss6dp0uoaje71fp8nkn3qsrmnxyahvu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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

On Thu, Nov 24, 2016 at 11:08 PM, <bri...@aracnet.com> wrote:

> On Fri, 25 Nov 2016 12:06:06 +0530
> Rahul Muttineni <rahulm...@gmail.com> 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
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Jeff Brown | Jeffrey Benjamin Brown
Website <https://msu.edu/~brown202/>   |   Facebook
<https://www.facebook.com/mejeff.younotjeff>   |   LinkedIn
<https://www.linkedin.com/in/jeffreybenjaminbrown>(I often miss messages
here)   |   Github <https://github.com/jeffreybenjaminbrown>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20161124/46a05aa5/attachment-0001.html>

------------------------------

Message: 5
Date: Thu, 24 Nov 2016 23:34:10 -0800
From: <bri...@aracnet.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161124233410.35ff4...@basalt.deldotd.com>
Content-Type: text/plain; charset=US-ASCII

On Thu, 24 Nov 2016 23:09:26 -0800
Jeffrey Brown <jeffbrown....@gmail.com> 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, <bri...@aracnet.com> wrote:
> 
> > On Fri, 25 Nov 2016 12:06:06 +0530
> > Rahul Muttineni <rahulm...@gmail.com> 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
> > Beginners@haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >  
> 
> 
> 



------------------------------

Message: 6
Date: Fri, 25 Nov 2016 08:41:46 +0000
From: Rein Henrichs <rein.henri...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID:
        <cajp6g8wrzpziuid4ncacv1u-uhudgoqmmvfrnezdwpcwfbe...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <bri...@aracnet.com> wrote:

On Thu, 24 Nov 2016 23:09:26 -0800
Jeffrey Brown <jeffbrown....@gmail.com> 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, <bri...@aracnet.com> wrote:
>
> > On Fri, 25 Nov 2016 12:06:06 +0530
> > Rahul Muttineni <rahulm...@gmail.com> 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
> > Beginners@haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >
>
>
>

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20161125/06186d98/attachment-0001.html>

------------------------------

Message: 7
Date: Fri, 25 Nov 2016 10:19:14 +0100
From: Daniel Trstenjak <daniel.trsten...@gmail.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161125091914.GA9587@octa>
Content-Type: text/plain; charset=iso-8859-1


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


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 101, Issue 10
******************************************

Reply via email to