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.  Parser using the show method (PICCA Frederic-Emmanuel)
   2. Re:  Parser using the show method (Tobias Brandt)
   3. Re:  Parser using the show method (Francesco Ariis)
   4. Re:  Parser using the show method (PICCA Frederic-Emmanuel)


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

Message: 1
Date: Fri, 21 Jun 2019 08:05:21 +0000
From: PICCA Frederic-Emmanuel
        <frederic-emmanuel.pi...@synchrotron-soleil.fr>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell" <beginners@haskell.org>
Subject: [Haskell-beginners] Parser using the show method
Message-ID:
        
<a2a20ec3b8560d408356cac2fc148e53015bbbc...@sun-dag3.synchrotron-soleil.fr>
        
Content-Type: text/plain; charset="us-ascii"

Hello,

I have a data type like this

data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | 
C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | 
P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | 
P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 
| P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | 
P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132
    deriving (Show)

has you can see it is quite long.

I want to write a method in order to parser a string using the show method.

myparser :: Parser SpaceGroup
myparser = do
    ...

I do not want to write for each constructor something like

do
   t <- takeText
   if t == "P21" then P21 else ...

thanks for your help

Frederic

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

Message: 2
Date: Fri, 21 Jun 2019 14:02:02 +0200
From: Tobias Brandt <to...@uni-bremen.de>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Parser using the show method
Message-ID: <7e877a3b-aa8f-5259-4819-f829824dc...@uni-bremen.de>
Content-Type: text/plain; charset=utf-8; format=flowed

Hello,

you may derive an Enum instance for your SpaceGroup type and then 
generate all enumerations:

data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | 
C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | 
P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | 
I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | 
P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 
| P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | 
F23 | F432 | F4132 | I23 | I213 | I432 | I4132
     deriving (Show, Enum)


matches t =  t `elem` map show [P1 .. I4132]

But of course this should not be as efficient as writing every 
constructor case by hand.


Tobias



On 6/21/19 10:05 AM, PICCA Frederic-Emmanuel wrote:
> Hello,
>
> I have a data type like this
>
> data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 
> | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 
> | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | 
> P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | 
> P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | 
> P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | 
> I432 | I4132
>      deriving (Show)
>
> has you can see it is quite long.
>
> I want to write a method in order to parser a string using the show method.
>
> myparser :: Parser SpaceGroup
> myparser = do
>      ...
>
> I do not want to write for each constructor something like
>
> do
>     t <- takeText
>     if t == "P21" then P21 else ...
>
> thanks for your help
>
> Frederic
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

Message: 3
Date: Fri, 21 Jun 2019 14:09:36 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Parser using the show method
Message-ID: <20190621120936.lmmpldlwupb3x...@x60s.casa>
Content-Type: text/plain; charset=us-ascii

Hello Frederic,

On Fri, Jun 21, 2019 at 08:05:21AM +0000, PICCA Frederic-Emmanuel wrote:
> Hello,
> 
> I have a data type like this
> 
> data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 
> | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 
> | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | 
> P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | 
> P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | 
> P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | 
> I432 | I4132
>     deriving (Show)
> 
> has you can see it is quite long.
> 
> I want to write a method in order to parser a string using the show method.

If you can use `read`

    data SpaceGroup = P1 | P2 | P21 | C2 | P222
                    deriving (Show, Read)

    p = read <$> takeText

and if you cannot

    import Text.Parsec
    import Text.Parsec.String
    import Text.Parsec.Char

    data SpaceGroup = P1 | P2 | P21 | C2 | P222
                    deriving (Enum, Show)

    pa :: Parser SpaceGroup
    pa = let ps = map (\s -> s <$ string (show s)) [P1 ..]
         in choice ps
            -- this need to be tweaked

Is this what you were looking for?
-F


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

Message: 4
Date: Fri, 21 Jun 2019 12:37:37 +0000
From: PICCA Frederic-Emmanuel
        <frederic-emmanuel.pi...@synchrotron-soleil.fr>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell" <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Parser using the show method
Message-ID:
        
<a2a20ec3b8560d408356cac2fc148e53015bbbc...@sun-dag3.synchrotron-soleil.fr>
        
Content-Type: text/plain; charset="us-ascii"

Hello

>    data SpaceGroup = P1 | P2 | P21 | C2 | P222
>                    deriving (Show, Read)
>
>    p = read <$> takeText

this is excalty what I wanted. In my case I use readMaybe.

Do you know if a version existe of readMaybe with Text instead of String

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 132, Issue 5
*****************************************

Reply via email to