gpt4 book ai didi

haskell - 不在范围 : data constructor `String' 内

转载 作者:行者123 更新时间:2023-12-02 21:26:22 26 4
gpt4 key购买 nike

我是 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)

最佳答案

您似乎忘记将 String 添加到您的 Value 数据类型?

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

关于haskell - 不在范围 : data constructor `String' 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14652382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com