fork download
  1. import Data.List
  2.  
  3. gen2to gen [ ] = Just [ ]
  4. gen2to gen ( x : xs ) = let
  5. lower = [ y | y <- xs, elem ( y, x ) gen ]
  6. higher = [ y | y <- xs, elem ( x, y ) gen ]
  7. newgen = [ ( y, z ) | y <- lower, z <- higher ]
  8. cont = [ ] /= ( intersect lower higher )
  9. in if cont
  10. then Nothing
  11. else do
  12. po' <- gen2to ( gen ++ newgen ) xs
  13. let ( lowerS, higherS ) = break ( flip elem higher ) po'
  14. return $ lowerS ++ [ x ] ++ higherS
  15.  
  16. main = do
  17. print $ gen2to sample1 "ABC"
  18. print $ gen2to sample2 "ABCD"
  19. print $ gen2to sample3 "ABCDE"
  20.  
  21. sample1 = [ ]
  22. ++ [ ( x , 'A' ) | x <- [ 'B', 'C' ] ]
  23. ++ [ ( x , 'B' ) | x <- [ 'C' ] ]
  24. ++ [ ( x , 'C' ) | x <- [ ] ]
  25.  
  26. sample2 = [ ]
  27. ++ [ ( x , 'A' ) | x <- "C" ]
  28. ++ [ ( x , 'B' ) | x <- "D" ]
  29. ++ [ ( x , 'C' ) | x <- "BD" ]
  30. ++ [ ( x , 'D' ) | x <- "" ]
  31.  
  32. sample3 = [ ]
  33. ++ [ ( x , 'A' ) | x <- "E" ]
  34. ++ [ ( x , 'B' ) | x <- "D" ]
  35. ++ [ ( x , 'C' ) | x <- "B" ]
  36. ++ [ ( x , 'D' ) | x <- "" ]
  37. ++ [ ( x , 'E' ) | x <- "B" ]
  38.  
Success #stdin #stdout 0.01s 5472KB
stdin
Standard input is empty
stdout
Just "CBA"
Just "DBCA"
Just "DBECA"