0

I am trying to write a function in Haskell that takes a table and pads up cells of each column according to the maximum size of a string in that column. The way I am doing this is by using the technique - tying the knot. Here is the function that I wrote:

type Row = [String]
type Table = [Row]
type Width = [Int]

aux :: (Width,Width) -> Table -> (Width,Table)
aux (x,m) [[]] = (m,[[]])
aux (x,m) (z:zs) = (y,t)
    where
        (xz,zs') = aux (x,m) zs
        y = zipWith max (map length z) xz
        z' = adjust x z
        t = z':zs'

pipedTable :: Table -> Table
pipedTable t = t'
    where
        (m,t') = aux (m,zeroWidth) t
            where
                zeroWidth = take (length $ head t) $ repeat 0

adjust :: Width -> Row -> Row
adjust [] [] = []
adjust (w:ws) (r:rs) = (r ++ s):(adjust ws rs)
    where
        s = take (w-(length r)) $ repeat ' '

When I load this module and try to feed some input to the function pipedTable eg.

*Main> pipedTable [["I","am"],["You","look"],["Fine","I"]]

it gives,

[*** Exception: pipedTable.hs:(6,1)-(12,26): Non-exhaustive patterns in function aux.

I do not understand where is the problem.

ErikR
  • 50,049
  • 6
  • 66
  • 121
Iguana
  • 147
  • 7

1 Answers1

2

Should this case for aux:

aux (x,m) [[]] = (m,[[]])

really be:

aux (x,m) [] = (m,[[]])

Otherwise, there is no case for when the second argument is the empty list.

ErikR
  • 50,049
  • 6
  • 66
  • 121