我是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)
答案 0 :(得分:8)
您似乎忘了将String
添加到Value
数据类型?
data Value = Number Int
| Bool Bool
| String String
deriving (Eq, Show)