Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be resistant to ordering?

Tags:

haskell

parsec

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.

like image 655
fho Avatar asked Feb 17 '23 02:02

fho


1 Answers

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)
like image 158
FunctorSalad Avatar answered Feb 18 '23 17:02

FunctorSalad