我正在尝试在 Haskell 中编写一个函数,该函数采用一个表格并根据该列中字符串的最大大小填充每列的单元格。我这样做的方法是使用技术 - 打结。这是我写的函数:
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 ' '
当我加载这个模块并尝试向函数 pipedTable 提供一些输入时,例如。
*Main> pipedTable [["I","am"],["You","look"],["Fine","I"]]
它给,
[*** Exception: pipedTable.hs:(6,1)-(12,26): Non-exhaustive patterns in function aux.
我不明白问题出在哪里。