4

我是 Haskell 的新手。我在包含“String”的几乎每一行代码中都得到了这个“不在范围内:数据构造函数‘String’”

请看看我的代码,让我知道我做错了什么。我真的很感谢你的帮助,谢谢。

import Data.Maybe

data Op = Add | Sub | Mul | Div | And | Or | Not | Eq | Less | Great
    deriving (Eq, Show)

data Exp = Literal Value
     | Primitive Op [Exp]
     | Variable String
     | If Exp Exp Exp
     | Let [(String, Exp)] Exp
    deriving (Show, Eq)

data Value = Number Int
       | Bool Bool
    deriving (Eq, Show)

type Env = [(String, Value)]

eval :: Env -> Exp -> Value
eval e (Literal v) = v
eval e (Variable x) = fromJust (lookup x e)
--22
prim :: op -> [Value] -> Value
prim Add [Number a, Number b] = Number (a+b)
prim And [Bool a, Bool b] = Bool (a && b)
prim Sub [Number a, Number b] = Number (a-b)
prim Mul [Number a, Number b] = Number (a*b)
prim Div [Number a, Number b] = Number (a/b)
prim Or [Bool a, Bool b] = Bool (a || b)
prim Not [Bool a] = Bool (not a)
prim Eq [Number a, Number b] = Bool (a == b)
prim Eq [String a, String b] = Bool (a == b) 
prim Less [Number a, Number b] = Bool (a < b)
prim Less [String a, String b] = Bool (a < b)
prim Great [Number a, Number b] = Bool (a > b)
prim Great [String a, String b] = Bool (a > b) 
4

1 回答 1

8

看起来您忘记添加String数据Value类型了?

data Value = Number Int
       | Bool Bool
       | String String
    deriving (Eq, Show)
于 2013-02-01T18:10:17.930 回答