Guest User

Untitled

a guest
Apr 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. {-
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. -}
  12.  
  13.  
  14.  
  15.  
  16. leaf x = Node x Null Null
  17. t = Node 17 (Node 12 (Node 5 Null (leaf 8)) (leaf 15)) (Node 1005 (Node 32 (leaf 30) (Node 46 Null (leaf 57))) (Node 1 Null (Node 2 Null (Node 3 Null Null))))
  18.  
  19.  
  20.  
  21. data Tree a = Null | Node a (Tree a) (Tree a) deriving (Show,Eq)
  22. data Box a = One a | Many [a] deriving (Show, Eq)
  23. data KeyPress = Delete | Backspace | Type Char deriving (Show, Eq)
  24.  
  25. ifMany (Many _) = True
  26. isMany _ = False
  27. isType (Type _) = True
  28. isType _ = False
  29.  
  30.  
  31. treeHeight (Node val l r) = (max (treeHeight l) (treeHeight r)) + 1
  32. treeHeight Null = 0
  33.  
  34. inorder (Node v left right) = ((inorder left) ++ [v] ++ (inorder right))
  35. inorder Null = []
  36.  
  37.  
  38. -- utilities
  39.  
  40. splitNum n = ((n `div` 2), (n `div` 2) + (n `mod` 2))
  41.  
  42. iter 0 f val = val
  43. iter n f val = iter (n-1) f (f val)
  44.  
  45. betterZip :: [[a]] -> [[a]]
  46. betterZip l = if any null l then [] else (map head l) : betterZip (map tail l)
  47.  
  48. pad c padval lst
  49.   | length lst == c = lst
  50.   | length lst < c  = pad c padval (lst ++ [padval])
  51.   | otherwise  = error("list bigger than padding")
  52.  
  53.  
  54.  
  55. (spaces, arrows, vert) = (create ' ', create '|', create '-') where create char s = pad s char "" -- replicate benutzen
  56.  
  57. replaceEvery every elem lst = [if index `mod` every == 0 then elem else charNow |
  58.   (index, charNow) <- zip [0..length(lst)-1] lst]
  59.  
  60. --mapShowOnPostorder = filter ((head.filter) isMany) rotatedTree
  61.  
  62.  
  63.  
  64. boxedTreeRepr :: (Show t) => Tree t -> [[Box Char]]
  65. boxedTreeRepr tree = (unrotateTree . (rotatedTree' 0)) tree
  66.  where
  67.    rotatedTree' :: (Show t) => Int -> Tree t -> [[Box Char]]
  68.     rotatedTree' c (Node val left right) =
  69.      (rotatedTree' (c+cIncr)  left) ++
  70.       [(map One (if c == 0 then "" else ((spaces (c-cIncr)) ++ "+" ++ (arrows (cIncr-1))))) ++ [Many (show val)] ]
  71.       ++ rotatedTree' (c+cIncr)  right
  72.    
  73.    rotatedTree' c Null =  []
  74.    
  75.     --maxShowLength = foldr max 0 (inorder tree)
  76.     cIncr = 3
  77.    
  78.     putSpaces (a:b:xs) = a : (replicate 5 []) ++ putSpaces (b:xs)
  79.     putSpaces  [x] = [x]
  80.     putSpaces [] = []
  81.    
  82.     unrotateTree myFlipped = (betterZip) (map myPad (putSpaces myFlipped))
  83.       where myPad = pad ((treeHeight t)*cIncr)  (One ' ')
  84.  
  85.  
  86.  
  87. expandBoxesIntoString = (typing2string.boxes2typing)
  88.   where
  89.  
  90.     boxes2typing :: [Box Char] -> [KeyPress]
  91.     boxes2typing (One a : rest) = Type a : boxes2typing rest
  92.     boxes2typing (Many as : rest) =  (replicate a Backspace) ++ (map Type as) ++ (replicate a Delete) ++ boxes2typing rest
  93.       where (a, b) = splitNum $ length as
  94.  
  95.     boxes2typing [] = []
  96.  
  97.     applyOne (Delete : Type _ : rest) = rest
  98.     applyOne (Type _ : Backspace : rest) = rest
  99.  
  100.     applyOne (Delete : Delete : rest) = Delete : (applyOne $ Delete:rest)
  101.     applyOne (a : b : rest) = a : (applyOne $ b:rest)
  102.     applyOne x = error $ "no matching for: " ++ show x
  103.  
  104.  
  105.     applyAllBackspaces typing = iter backspaces applyOne typing
  106.       where backspaces = length $ filter (not.isType) typing
  107.  
  108.     typing2string typing = map (\(Type x)->x) (applyAllBackspaces typing)
  109.  
  110.  
  111.  
  112. myShow tree = unlines $ map (dontPutMinus.expandBoxesIntoString) (boxedTreeRepr t) -- naja soll nur bei jeden drittten element angewendet werden, aber die funktion putMinus und dontPutMinus ist falsch und ich hab kein bock mehr
  113.  
  114.  
  115. putMinus ('+':xs) = '+':dontPutMinus xs
  116. putMinus (' ':xs)   = '-':putMinus xs
  117. putMinus (x:xs)     =  x:putMinus xs
  118. putMinus [] = []
  119.  
  120. dontPutMinus ('+':xs) = '+':putMinus xs
  121. dontPutMinus (' ':xs)   = ' ':dontPutMinus xs
  122. dontPutMinus (x:xs)     =  x:dontPutMinus xs
  123. dontPutMinus [] = []
  124.  
  125. {-
  126. Beachte wie das ergebnis falsch ist, aber der ansatz ist ok
  127.  
  128. *Main> putStr $ myShow t
  129.             +----------17----------------------------+                  
  130.             |                                         |                  
  131.             |                                         |                  
  132. +----------12----+                 +---------------1005---+            
  133. |                 |                 |                       |            
  134. |                 |                 |                       |            
  135. 5     +----------15----------+    32    +-----------------1-----+      
  136.       |                       |           |                       |      
  137.       |                       |           |                       |      
  138.       8                      30         46    +-----------------2-----+
  139.                                                 |                       |
  140.                                                 |                       |
  141.                                                57                      3
  142.                                                                          
  143.    
  144.  
  145.  
  146.  
  147. -}
Add Comment
Please, Sign In to add comment