我是Haskell的新手,我已經四處尋找答案,但沒有運氣。Haskell - 組合數據類型?
這段代碼爲什麼不編譯?
newtype Name = Name String deriving (Show, Read)
newtype Age = Age Int deriving (Show, Read)
newtype Height = Height Int deriving (Show, Read)
data User = Person Name Age Height deriving (Show, Read)
data Characteristics a b c = Characteristics a b c
exampleFunction :: Characteristics a b c -> User
exampleFunction (Characteristics a b c) = (Person (Name a) (Age b) (Height c))
錯誤:
"Couldn't match expected type ‘String’ with actual type ‘a’,‘a’ is a rigid type, variable bound by the type signature"
然而,這個編譯就好:
exampleFunction :: String -> Int -> Int -> User
exampleFunction a b c = (Person (Name a) (Age b) (Height c))
我意識到人們做上述的簡單的方法,但我只是測試的不同用途自定義數據類型。
更新:
我的傾向是,編譯器不喜歡 'exampleFunction ::特性A B C',因爲它不是類型安全的。即我不提供以下保證:a ==姓名字符串,b == Age Age,c ==高度Int。
謝謝,我剛剛更新了我的問題,同時有相當多這樣:P –