Guest User

Untitled

a guest
Jul 22nd, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. align :: String -> String
  4. align code = unlines $ map ("   "++)  (lines code)
  5.  
  6. cIf c t e = "if(" ++ c++ "){\n" ++ align t ++ "} else {\n" ++ align e ++ "}\n"
  7.  
  8. tableSmall :: [(String, String)] -> String
  9. tableSmall t = "switch(x){\n" ++ (align $ unlines $ map l t ++ ["default: return -1;"]) ++ "}\n"
  10.     where l (x, y) = "case " ++ x ++ ": return " ++ y ++ ";"
  11.  
  12. table :: [(String, String)] -> String
  13. table t | length t <=4   = tableSmall t
  14.         | otherwise = cIf ("x<="++pivot) (table ht1) (table ht2)
  15.             where xs = map fst t
  16.                   ys = map snd t
  17.                   n = length xs
  18.                   ht1 = take (n `div` 2) t
  19.                   ht2 = drop (n `div` 2) t
  20.                   pivot = fst $ last ht1
  21.          
  22.  
  23. tableFunction name t = "int " ++ name ++ "(int x){\n" ++ align (table st) ++ "}"
  24.     where st = map (\(a, b) -> (show a, show b)) (sort t)
Advertisement
Add Comment
Please, Sign In to add comment