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. Re:  pattern matching on a common element (bri...@aracnet.com)
   2. Re:  pattern matching on a common element (bri...@aracnet.com)
   3. Re:  pattern matching on a common element (Daniel Trstenjak)
   4. Re:  pattern matching on a common element (bri...@aracnet.com)


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

Message: 1
Date: Fri, 25 Nov 2016 08:08:35 -0800
From: <bri...@aracnet.com>
To: Rein Henrichs <rein.henri...@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: <20161125080835.44461...@basalt.deldotd.com>
Content-Type: text/plain; charset=US-ASCII

On Fri, 25 Nov 2016 08:41:46 +0000
Rein Henrichs <rein.henri...@gmail.com> 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


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

Message: 2
Date: Fri, 25 Nov 2016 08:20:29 -0800
From: <bri...@aracnet.com>
Cc: beginners@haskell.org
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161125082029.2ca60...@basalt.deldotd.com>
Content-Type: text/plain; charset=UTF-8

On Fri, 25 Nov 2016 10:19:14 +0100
Daniel Trstenjak <daniel.trsten...@gmail.com> 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



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

Message: 3
Date: Fri, 25 Nov 2016 18:18:21 +0100
From: Daniel Trstenjak <daniel.trsten...@gmail.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161125171821.GA4138@octa>
Content-Type: text/plain; charset=us-ascii


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


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

Message: 4
Date: Fri, 25 Nov 2016 12:50:01 -0800
From: <bri...@aracnet.com>
To: Daniel Trstenjak <daniel.trsten...@gmail.com>
Cc: beginners@haskell.org
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <20161125125001.4e09c...@basalt.deldotd.com>
Content-Type: text/plain; charset=US-ASCII

On Fri, 25 Nov 2016 18:18:21 +0100
Daniel Trstenjak <daniel.trsten...@gmail.com> 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


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

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 11
******************************************

Reply via email to