问题描述
我得到这个错误,我不知道我做错了什么。
我是Haskell的新手,所以请为我详细阐述一切。
I am getting this error, and I don't know what I am doing wrong.I am new to Haskell, so please elaborate everything for me.
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
| String String
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 `div` 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) --37
main = do
n = "n" -- parse error on input `='
nv = Variable "n" -- parse error on input `='
lit n = Literal (Number n)
t0 = Primitive Mul [lit 5, lit 2]
t1 = Let [(n, t0)] (If (Primitive Great [nv, lit 9]) (lit 1) (lit 0)) -- parse error on input `='
我在评论中写道我得到的错误。
我检查了参数的有效性,一切看起来都很好。这可能是我的语法错误......我想。
I wrote in comments where I am getting the errors.I checked the validness of the parameters, and everything looks fine. It's maybe an error with my syntax... I guess.
预先感谢您。
推荐答案
你的主要功能几乎在任何方面都被破坏了。你是否学过单子或者仅仅是关于IO monad的情况?
Your main function is broken in almost every way. Have you learned monads or for this case only about the IO monad?
main :: IO Exp
main = do
let n = "n"
nv = Variable "n"
lit n = Literal (Number n)
t0 = Primitive Mul [lit 5, lit 2]
in return $ Let [(n, t0)] (If (Primitive Great [nv, lit 9]) (lit 1) (lit 0))
只是在do-Block的上下文中使用 =
。
此版本按预期工作。了解 let
表达式和 return
。如果您不知道这一点,请阅读。
You can't just use =
in the context of that do-Block.This version works as intended. Learn about the let
expression and the return
. If you don't know this, read for example here.
这篇关于Haskell:解析输入错误'='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!