Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- align :: String -> String
- align code = unlines $ map (" "++) (lines code)
- cIf c t e = "if(" ++ c++ "){\n" ++ align t ++ "} else {\n" ++ align e ++ "}\n"
- tableSmall :: [(String, String)] -> String
- tableSmall t = "switch(x){\n" ++ (align $ unlines $ map l t ++ ["default: return -1;"]) ++ "}\n"
- where l (x, y) = "case " ++ x ++ ": return " ++ y ++ ";"
- table :: [(String, String)] -> String
- table t | length t <=4 = tableSmall t
- | otherwise = cIf ("x<="++pivot) (table ht1) (table ht2)
- where xs = map fst t
- ys = map snd t
- n = length xs
- ht1 = take (n `div` 2) t
- ht2 = drop (n `div` 2) t
- pivot = fst $ last ht1
- tableFunction name t = "int " ++ name ++ "(int x){\n" ++ align (table st) ++ "}"
- where st = map (\(a, b) -> (show a, show b)) (sort t)
Advertisement
Add Comment
Please, Sign In to add comment