由于a former question,我一直在研究haskell中的硬数据类型和类型类,我想知道是否有某个类和数据类型,
例如:
class Example a where
function :: a -> Int
和
data Tree a = EmptyTree
| Node a (Tree [a]) (Tree [a]) deriving (Show, Read, Eq)
如何在数据类型树的类示例中使用该函数?
我认为它与实例有关,但这是正确的吗?
instance Example where
function :: a -> Int, and then i define the function here?
你能举个例子吗?
答案 0 :(得分:1)
您的实例应如下所示:
instance Example (Tree a) where
function EmptyTree = ...
function (Node val left right) = ...
答案 1 :(得分:1)
从你最近提出的另一个问题来看,我认为你想要这样的事情:
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 ...