[Haskell-beginners] pattern matching on a common element

Rahul Muttineni rahulmutt at gmail.com
Fri Nov 25 06:36:06 UTC 2016


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, <briand at 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 at 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.html>


More information about the Beginners mailing list