我正在努力提高对 Haskell 中代数数据类型的理解。这个例子没有多大意义,但我只是想学习一些东西。不过,代码应该是不言自明的!假设我有一些代码:
type Pig = String
type Lion = String
type Feed = [(Char,Char)]
type Visitors = [(Char,Char)]
type Costs = (Int,Int,Int)
data AnimalHome = Farm Pig Pig Pig Feed | Zoo Lion Lion Lion Feed Visitors
orders :: Char -> AnimalHome -> Costs -> Char
orders stuff Farm bert donald horace specialFeed (cost1,cost2,cost3) = some code here
到目前为止这是正确的,还是我做错了一些事情?
最佳答案
正如 @GabrielGonzalez 已经建议的那样,您需要将 Farm bert donald horacespecialFeed
放在括号中并处理 Zoo
情况:
orders :: Char -> AnimalHome -> Costs -> Char
orders stuff (Farm bert donald horace specialFeed) (cost1,cost2,cost3) = undefined
orders stuff (Zoo l1 l2 l3 feed visitors) (c1, c2, c3) = undefined
关于haskell - 声明我自己的类型并在 Haskell 的函数中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059524/