Wou*_*tte 0 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和其他东西?"k"来自索引中的哪里?为什么一些函数的输出是(ik)?这甚至不是一种类型.
将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
您现在应该能够填写实际的定义.