Question

Given input in form A,B,C where the order is not fixed (eg C,B,A might be possible too), how would I parse the string into a tuple (a,b,c) so that the result is ordered again?

Bigger example:

Input A:

A 1
B 2
C 3

Input B:

C 3
B 2
A 1

Parsers:

a = "A " *> decimal
b = "B " *> decimal
c = "C " *> decimal

How do I write a parser that will give (1,2,3) for Input A and B.

Edit: I have to more exact: they aren't all decimals ... Else choice would be the combinator of choice here.

Était-ce utile?

La solution

You can use Text.Parsec.Perm:

{-# LANGUAGE TupleSections, NoMonomorphismRestriction #-}

import Text.Parsec
import Text.Parsec.Perm
import Control.Applicative

a = ...
b = ...
c = ...

p = permute ( 
      (,,) <$$> a <||> b <||> c)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top