yingpotter

[99p]Multi(Complete)

Jul 27th, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. -- predefines
  4. data Mtree a = Node a [Mtree a] deriving (Eq, Show)
  5. tree1 = Node 'a' []
  6. tree2 = Node 'a' [Node 'b' []]
  7. tree3 = Node 'a' [Node 'b' [Node 'c' []]]
  8. tree4 = Node 'b' [Node 'd' [], Node 'e' []]
  9. tree5 = Node 'a' [
  10.                  Node 'f' [Node 'g' []],
  11.                  Node 'c' [],
  12.                  Node 'b' [Node 'd' [], Node 'e' []]
  13.                  ]
  14.              
  15. -- problem 70c
  16. mnodecount :: Mtree a -> Int
  17. mnodecount (Node _ []) = 1
  18. mnodecount (Node _ xs) = 1 + (foldr (+) 0 $ map mnodecount xs)
  19.  
  20. -- problem 70
  21. mtreeStr :: Mtree Char -> String
  22. mtreeStr (Node a []) = [a] ++ "^"
  23. mtreeStr (Node a (x:xs)) = [a] ++ mtreeStr x ++ (concatMap mtreeStr xs) ++ "^"
  24. ctrnoctr :: String -> Int
  25. ctrnoctr "" = 0
  26. ctrnoctr (x:xs) = (if x == '^' then (-1) else 1) + ctrnoctr xs
  27. mspSearch :: String -> [Int]
  28. mspSearch "" = []
  29. mspSearch xs = [-1] ++ (findIndices (==0) $ map ctrnoctr (tail $ inits xs))
  30. diffMsp :: [Int] -> [Int]
  31. diffMsp [] = []
  32. diffMsp [x] = []
  33. diffMsp (x:y:xs) = (y-x) : diffMsp(y:xs)
  34. mspList :: [Int] -> String -> [String]
  35. mspList [] _ = []
  36. mspList ns [] = []
  37. mspList (n:ns) xs = (take n xs) : mspList ns (drop n xs)
  38. strMtree :: String -> Mtree Char
  39. strMtree (x:y:xs)
  40.           | y == '^'   = Node x []
  41.           | y /= '^'   = Node x $map strMtree (mspList ns (y:init xs))
  42.                                   where ns = diffMsp $ mspSearch (y:init xs)
  43.  
  44. -- problem 71
  45. ipl' :: Int -> Mtree a -> Int
  46. ipl' _ (Node a []) = 0
  47. ipl' n (Node a ts) = n*(length ts) + sum (map (ipl' (n+1)) ts)
  48. iplMtree :: Mtree a -> Int
  49. iplMtree = ipl' 1
  50.  
  51. -- problem 72
  52. buMtree :: Mtree Char -> String
  53. buMtree (Node a []) = [a]
  54. buMtree (Node a xs) = (concatMap buMtree xs) ++ [a]
  55.  
  56. -- problem 73
  57. mtreeLtree :: Mtree Char -> String
  58. mtreeLtree (Node a []) = [a]
  59. mtreeLtree (Node a xs) = "(" ++ [a] ++ (concatMap (" "++) $ map mtreeLtree xs) ++ ")"
  60. ----- mtreeLtree (Node a xs) = "(" ++ [a] ++ (concatMap mtreeLtree xs) ++ ")"
  61. countparaLeft :: String -> Int
  62. countparaLeft [] = 0
  63. countparaLeft xs = length $ findIndices (=='(') xs
  64. countparaRight :: String -> Int
  65. countparaRight [] = 0
  66. countparaRight xs = length $ findIndices (==')') xs
  67. mdpSearch :: String -> [Int]
  68. mdpSearch xs = [i | i <- findIndices (==' ') xs, countparaLeft (take i xs) == countparaRight (take i xs)]
  69.               ++ [length xs]
  70. diffMdp :: [Int] -> [Int]
  71. diffMdp [] = []
  72. diffMdp [x] = []
  73. diffMdp (x:y:xs) = (y-x) : diffMdp(y:xs)
  74. mdpList :: [Int] -> String -> [String]
  75. mdpList [] _ = []
  76. mdpList ns [] = []
  77. mdpList (n:ns) xs = (tail $take n xs) : mdpList ns (drop n xs)
  78. ltreeMtree :: String -> Mtree Char
  79. ltreeMtree [a] = Node a []
  80. ltreeMtree (x:y:xs)
  81.           |x/='('  = error "dont kidding me"
  82.           |x=='('  = Node y $ map ltreeMtree (mdpList ns $ init xs)
  83.                                   where ns = diffMdp $ mdpSearch (init xs)
Advertisement
Add Comment
Please, Sign In to add comment