簡體   English   中英

實例聲明Haskell

[英]Instance Declaration 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 }

有任何想法嗎?

elemIndex要求列表中的元素具有相等性,因此您需要為Eq Inst添加一個實例。

使用deriving Eq使其自動生成,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM