我有自己的数据类型,说明:

data Commands = MoveLeft |
                MoveRight |
                MoveUp |
                MoveDown |
                IfVertical |
                IfHorizontal |
                InputChar |
                InputInt |
                OutputChar |
                OutputInt |
                OutputNewline |
                PushInt Int |
                Add |
                Sub |
                Mult |
                Div |
                Exp |
                Pop |
                Dup |
                Switch |
                Noop |
                End
                deriving (Show, Eq)

我有一个函数,我试图用它从 PushInt 中提取数字:
extractNum :: PushInt -> Int
extractNum (PushInt n) = n

但是当我尝试运行它时,我收到一条错误消息:
Parser.hs:32:19:
    Not in scope: type constructor or class `PushInt'
    A data constructor of that name is in scope; did you mean -XDataKinds?

据我所知,我被允许使用这种方法从数据中提取一个字段。我很确定这只是一个非常简单的错误,但任何帮助表示赞赏。

最佳答案

哇,我对凌晨 2 点的错误是正确的。功能

extractNum :: PushInt -> Int
extractNum (PushInt n) = n

应该
extractNum :: Commands -> Int
extractNum (PushInt n) = n

10-08 08:25