【发布时间】:2015-02-25 11:23:30
【问题描述】:
假设我有以下数据类型:
data BinaryTree a = EmptyTree | Node a (BinaryTree a) (BinaryTree a) deriving (Eq, Ord, Show)
还有下面的函数
member a EmptyTree = False
member a (Node x l r)
| a < x = member a l
| a > x = member a r
| a == x = True
此功能有效。
检查Node的类型:
:t Node
Node :: a -> BinaryTree a -> BinaryTree a -> BinaryTree a
但是,如果给成员函数一个签名:
member :: Node a -> BinaryTree a -> Bool
(a 类型的Node 和a 类型的BinaryTree 产生Bool)
它出错了:
Not in scope: type constructor or class ‘Node’
A data constructor of that name is in scope; did you mean DataKinds?
这是为什么呢?如何定义一个接受(并比较)任意类型的节点和树的函数?
【问题讨论】:
-
Node不是类型,而是值。你想要member :: BinaryTree a -> BinaryTree a -> Bool。 -
我觉得应该是
member :: a -> BinaryTree a -> Bool -
@user3585010 呃,是的,看起来你是对的。
标签: haskell types binary-search-tree membership