Haskell数据类型未知的数据类型

时间:2017-12-14 12:35:33

标签: haskell types functional-programming

对于作业,给出了以下代码

-- 2. Index

class Index i where
  findEntry :: Eq k => k -> i k -> Maybe Entry
  empty     :: Eq k => i k
  singleton :: Eq k => k -> Entry -> i k
  (<+>)     :: Eq k => i k -> i k -> i k

-- a. Complete the definition of Assoc
data Assoc k
  = MkAssoc [(k,Entry)]
  deriving (Eq,Show)

-- b. Complete the instance of Index for Assoc
instance Index Assoc where

我现在完全陷入了问题2.b.如何制作空的findEntry和其他东西? &#39; k&#39;来自索引?为什么一些函数的输出是(i k)?那甚至都不是一种类型。

1 个答案:

答案 0 :(得分:6)

i中的class Index i代表种类* -> *的类型构造函数。也就是说,i可以是Maybe[]IO。更多内容i也可以是Assoc

请注意i本身不是一个类型,但类型构造函数(如Assoc)本身不是一个类型。相反,i k类似于Assoc k类型。

您需要编写的实例具有以下定义方法:

instance Index Assoc where
  findEntry :: Eq k => k -> Assoc k -> Maybe Entry
  empty     :: Eq k => Assoc k
  singleton :: Eq k => k -> Entry -> Assoc k
  (<+>)     :: Eq k => Assoc k -> Assoc k -> Assoc k

您现在应该能够填写实际的定义。