实例声明Haskell

时间:2014-04-01 13:07:31

标签: haskell

我的代码是:

type Code = [Inst]

data Inst = PUSH Int
          | PUSHV Name
          | POP Name
          | DO Op
          | JUMP Label
          | JUMPZ Label
          | LABEL Label
          deriving Show

doJump :: Inst -> Code -> Code
doJump l c = case (elemIndex l c) of
                 Just n -> drop (n+1) c
                 Nothing -> c

GHC给我这个错误,这让我完全难过...... 我想要做的是从特定元素发生后的点返回列表。

No instance for (Eq Inst) arising from a use of `elemIndex'
Possible fix: add an instance declaration for (Eq Inst)
In the expression: (elemIndex l c)
In the expression:
  case (elemIndex l c) of {
    Just n -> drop (n + 1) c
    Nothing -> c }
In an equation for `doJump':
    doJump l c
      = case (elemIndex l c) of {
          Just n -> drop (n + 1) c
          Nothing -> c }

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

elemIndex要求列表中的元素具有可比性,因此您需要为Eq Inst添加实例。

使用deriving Eq自动生成,例如:

data Inst = PUSH Int
          | PUSHV Name
          | POP Name
          | DO Op
          | JUMP Label
          | JUMPZ Label
          | LABEL Label
          deriving (Show, Eq)