[英]Haskell Data Types and type classes
Due to a former question , I've been studying hard data types and type classes in haskell, and I want to know if a have a certain class and a data type, 由于前一个问题 ,我一直在研究haskell中的硬数据类型和类型类,并且我想知道a是否具有特定的类和数据类型,
For example: 例如:
class Example a where
function :: a -> Int
and 和
data Tree a = EmptyTree
| Node a (Tree [a]) (Tree [a]) deriving (Show, Read, Eq)
How can I use the function in the class example in the data type tree? 如何在数据类型树的类示例中使用该函数?
I think it has something to do with instances, but is this correct? 我认为这与实例有关,但这是正确的吗?
instance Example where
function :: a -> Int, and then i define the function here?
Can you give me an example? 你能举个例子吗?
Your instance implementation should look like: 您的实例实现应如下所示:
instance Example (Tree a) where
function EmptyTree = ...
function (Node val left right) = ...
Judging from the other question you asked lately, I think you want something like this: 从您最近提出的另一个问题来看,我认为您想要这样的事情:
class Order a where
order :: a -> Int
insert :: Order k => k -> Tree k -> Tree k
insert key tree
| order key == 0 = ..do work for items of order 0 ...
| otherwise = ..do work for items of higher order ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.