我是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 –