1

由于以前的问题,我一直在研究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? 

能给我举个例子?

4

2 回答 2

1

您的实例实现应如下所示:

instance Example (Tree a) where
  function EmptyTree = ...
  function (Node val left right) = ...
于 2013-11-07T10:19:46.093 回答
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 ...
于 2013-11-07T11:36:08.863 回答