haskell - 声明我自己的类型并在 Haskell 的函数中使用它们

标签 haskell types algebraic-data-types

我正在努力提高对 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/

相关文章:

haskell - 尝试实现 Data.Either

scala - 对泛型 Scala 函数中变量类型看似多余的重新指定感到困惑

types - 编译器如何确定sml中的类型

c - C 中的类型检查

functional-programming - 在 OCaml 中使用 GADT 时,如何删除函数上的 'less general definition' 错误消息?

haskell - 在模式保护或 case 表达式中重用模式

haskell - 如何使用 Haskell 动态调用函数

haskell - Haskell 中的种类文字

algebraic-data-types - 为什么和类型称为和类型?

haskell - Haskell 中可遍历