[Haskell-beginners] Parser using the show method

Tobias Brandt to_br at uni-bremen.de
Fri Jun 21 12:02:02 UTC 2019


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 at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


More information about the Beginners mailing list