简体   繁体   English

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

[英]Haskell Datatypes with unknown types

For an assignment, the following codes was given 对于作业,给出了以下代码

-- 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

I'm now completely stuck at question 2.b. 我现在完全陷入问题2.b。 How do I make the empty and findEntry and the other things? 我如何将其设为空和findEntry等? Where does the 'k' come from in index? 索引中的“ k”来自哪里? How come the output of some functions is (ik)? (ik)为什么有些函数的输出是多少? That't not even a type. 那甚至都不是类型。

The i in class Index i stands for a type constructor of kind * -> * . iclass Index i代表一种类型构造* -> * That is, i can be something like Maybe , [] , IO . 也就是说, i可以像Maybe[]IO More to the point i can also be Assoc . 更重要的是, i也可以是Assoc

Note that i on its own is not a type, but a type constructor, like Assoc on its own is not a type. 请注意, i本身不是类型,但是类型构造函数(例如Assoc本身)不是类型。 Instead ik is a type like Assoc k is a type. 相反, ik是类似Assoc k的类型, Assoc k是一种类型。

The instance you need to write has the following methods to be defined: 您需要编写的实例具有以下定义的方法:

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

You should now be able to fill in the actual definitions. 您现在应该能够填写实际的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM