Formatter: Rewrite code that used an n + k pattern
[match/match.git] / program / Formatter.hs
1 module Formatter where
2 import Data.List
3
4 #if __GLASGOW_HASKELL__ <= 606
5 intercalate xs xss = concat (intersperse xs xss)
6 #endif
7
8 padWith :: a -> Int -> [a] -> [a]
9 padWith _ 0 l = l
10 padWith e n [] = replicate n e
11 -- http://hackage.haskell.org/trac/haskell-prime/wiki/RemoveNPlusK
12 -- Ugg... GHC could at least give a meaningful error message. ~ Matt 2011-08-27
13 padWith e n (h:t) =
14         if n == 0 then error "padWith: list is already longer than the requested length"
15         else h:(padWith e (n-1) t)
16
17 formatTable :: [[String]] -> String
18 formatTable cells =
19         let columnWidths = map (\col -> maximum $ map length col)
20                 $ transpose cells in
21         intercalate "\n" $
22                 map (\row ->
23                         let rowCells = zipWith (padWith ' ') columnWidths row in
24                         intercalate " " rowCells
25                 ) cells