Skip to content

Commit

Permalink
Add initial MdTable
Browse files Browse the repository at this point in the history
  • Loading branch information
IvoTod committed Nov 29, 2016
1 parent d7707ad commit 5fbf67c
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions exercises/md-table/MdTable.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{-# OPTIONS_GHC -Wall #-}

module MdTable where

import Data.List (intercalate)

getMaxColumns :: [[String]] -> Int
getMaxColumns [] = 0
getMaxColumns xs = maximum [length x | x <- xs]

getMaxColumnLength :: [[String]] -> Int
getMaxColumnLength [] = 0
getMaxColumnLength xs = maximum [length x | x <- concat xs]

addEmptyStrings :: [String] -> Int -> [String]
addEmptyStrings xs 0 = xs
addEmptyStrings [] n = "" : (addEmptyStrings [] (n-1))
addEmptyStrings (x:xs) n = x : (addEmptyStrings xs (n))

equalizeColumnCount :: [[String]] -> [[String]]
equalizeColumnCount xs = [addEmptyStrings x (getMaxColumns xs - length x) | x <- xs]

equalizeColumnSize :: [[String]] -> [[String]]
equalizeColumnSize [] = []
equalizeColumnSize (x:xs) = [pad y | y <- x] : (equalizeColumnSize xs)
where pad z = padWith ' ' (getMaxColumnLength (x:xs)) z


toLine :: [String] -> String
toLine [] = "|"
toLine (x:xs) = "| " ++ x ++ " " ++ toLine xs


padWith :: (Eq a) => a -> Int -> [a] -> [a]
padWith _ 0 x | x == [] = []
| otherwise = x
padWith pad n [] = pad : padWith pad (n-1) []
padWith pad n (x:xs) = x : padWith pad (n-1) xs


layoutTable :: [[String]] -> String
layoutTable xs = intercalate "\n" [toLine x | x <- ys]
where ys = equalizeColumnSize (equalizeColumnCount xs)

table2 :: [[String]]
table2 = [
[ "Item", "Price" ],
[ "iPhone", "$1", "Not in stock" ],
[ "iPad", "$599" ],
[ "CAUTION: It's a scam!"]
]

0 comments on commit 5fbf67c

Please sign in to comment.