fork download
  1.  
  2.  
  3.  
  4. data DP = Z | L DP | R DP deriving Show
  5.  
  6. ic::DP->Maybe DP
  7. ic Z = Nothing
  8. ic (L n) = Just( R n )
  9. ic (R n) = L <$> (ic n)
  10.  
  11. acc:: DP -> Int
  12. acc Z = 0
  13. acc (L n) = (acc n) + 1
  14. acc (R n) = (acc n) + 1
  15.  
  16. add:: DP -> DP -> Maybe DP
  17.  
  18. add Z _ = Just Z
  19. add _ Z = Just Z
  20.  
  21. add x y
  22. | ( acc x ) < ( acc y ) = case y of
  23. (L z) -> add x z
  24. (R z) -> add x z
  25.  
  26. | ( acc x ) < ( acc y ) = case x of
  27. (L z) -> add z y
  28. (R z) -> add z y
  29.  
  30. | otherwise = case y of
  31. (L z) -> case x of
  32. (L w) -> L <$> (add z w)
  33. (R w) -> R <$> (add z w)
  34. Z -> Nothing
  35.  
  36. (R z) -> case (ic x) of
  37. Just (L w) -> L <$> (add z w)
  38. Just (R w) -> R <$> (add z w)
  39. Nothing -> Nothing
  40.  
  41.  
  42. fromstr :: String -> DP
  43. fromstr ('L' : xs)= L (fromstr xs)
  44. fromstr ('R' : xs)= R (fromstr xs)
  45. fromstr ('Z' : xs)= Z
  46. fromstr (_ : xs)= fromstr xs
  47. fromstr [] = Z
  48.  
  49. main = do {
  50. x <- (fromstr <$> getLine);
  51. y <- (fromstr <$> getLine);
  52. print ( add x y );
  53. }
Success #stdin #stdout 0s 4452KB
stdin
LRL
RRL
stdout
Just (R (L (R Z)))