[Haskell-beginners] Parser using the show method

Francesco Ariis fa-ml at ariis.it
Fri Jun 21 12:09:36 UTC 2019


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


More information about the Beginners mailing list