Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
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 ([email protected])
2. Re: pattern matching on a common element ([email protected])
3. Re: pattern matching on a common element (Daniel Trstenjak)
4. Re: pattern matching on a common element ([email protected])
----------------------------------------------------------------------
Message: 1
Date: Fri, 25 Nov 2016 08:08:35 -0800
From: <[email protected]>
To: Rein Henrichs <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Fri, 25 Nov 2016 08:41:46 +0000
Rein Henrichs <[email protected]> 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: <[email protected]>
Cc: [email protected]
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Fri, 25 Nov 2016 10:19:14 +0100
Daniel Trstenjak <[email protected]> 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 <[email protected]>
To: [email protected]
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: <[email protected]>
To: Daniel Trstenjak <[email protected]>
Cc: [email protected]
Subject: Re: [Haskell-beginners] pattern matching on a common element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Fri, 25 Nov 2016 18:18:21 +0100
Daniel Trstenjak <[email protected]> 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
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 101, Issue 11
******************************************