我有下面的代碼,使用this tabulation library:Text.Tabular.Table類型錯誤 - 類型系統向我詢問的是什麼?
{-# LANGUAGE NoImplicitPrelude, OverloadedStrings #-}
import ClassyPrelude
import qualified Text.Tabular as T
data Category = Age | Gender | Usual | Years
deriving (Show, Read, Eq, Enum, Bounded)
tabulate :: Text -> [[Int]] -> T.Table (T.Header Int) (T.Header Text) Int
tabulate lbl tab = T.Table (T.Group T.NoLine (map T.Header leftcoll)) (T.Group T.DoubleLine [T.Header lbl, T.Header "All", T.Header "Cluster0", T.Header "Cluster1"]) rest
where leftcoll = map (`indexEx` 0) tab
rest = map (drop 1) tab
當我嘗試編譯它,我得到以下錯誤:
Couldn't match type ‘Int’ with ‘T.Header Int’
Expected type: [T.Header Int]
Actual type: [Int]
In the second argument of ‘map’, namely ‘leftcoll’
In the second argument of ‘T.Group’, namely
‘(map T.Header leftcoll)’
Couldn't match expected type ‘T.Header Text’
with actual type ‘Text’
In the first argument of ‘T.Header’, namely ‘lbl’
In the expression: T.Header lbl
我絕對不知道爲什麼是這樣的情況。從the Table type documentation and example看來,我只需將Header
構造函數應用於值列表,然後將它們放入構造函數的構造函數中即可獲得整行(或列)的值,但這些錯誤似乎表明列表(和數據)我作爲參數傳遞給Header
構造函數已經需要爲Header
s。因此,我對這裏的類型系統告訴我什麼以及如何得到我想要的東西感到困惑。
從本質上講,這個表應該是這個樣子:
Foo All Cluster0 Cluster1
=========================
1 10 3 7
2 11 10 1
....