Haskell,我一直得到“输入`='上的解析错误”

时间:2013-02-13 02:42:58

标签: haskell

我正在完成作业。 我不知道为什么会收到这个错误。

我通过评论指出了错误来自的行。

如果你能(你不必),我不认为我的funccall功能正在发挥作用。

import Data.Maybe 

data Operator = Add | Sub | Mul | Div | And | Or | Not | Eq | Less | Great
  deriving (Eq, Show)
data Expression = Lit Value
         | Prim Operator [Expression]
         | Variable String
         | If Expression Expression Expression
         | Let [(String, Expression)] Expression
     | Func [String] Expression
     | Call Expression [Expression]
  deriving (Show, Eq)

data Value = Num Int
           | Bool Bool
       | Closure [String] Expression Environment
  deriving (Eq, Show)

type Environment = [(String, Value)]
--20
prim :: Operator -> [Value] -> Value
prim Add [Num a, Num b] = Num (a+b)
prim Mul [Num a, Num b] = Num (a*b)
prim Sub [Num a, Num b] = Num (a-b)
prim Div [Num a, Num b] = Num (a `div` b)
prim And [Bool a, Bool b] = Bool (a && b)
prim Or [Bool a, Bool b] = Bool (a || b)
prim Not [Bool a] = Bool (not a)
prim Eq [a, b] = Bool (a == b)
prim Less [Num a, Num b] = Bool (a < b)
prim Great [Num a, Num b] = Bool (a > b)
--32
evaluate :: Environment -> Expression -> Value
evaluate e (Lit v)  = v
evaluate e (Prim op as) = prim op (map (evaluate e) as)
evaluate e (Variable x) = fromJust (lookup x e)
evaluate e (If a b c)   = evaluate e (if fromBool (evaluate e a) then b else c)
evaluate e (Let bs b)   = evaluate ([(x, evaluate e d) | (x,d) <- bs ] ++ e) b

eval e (Func str ex) = (Closure str ex e) --40
evaluate e (Call ex exl) = let (Closure a b c) = (evaluate e ex) 
            in (Closure a b (c ++ (helper a (map (evaluate e) exl)))) 

helper :: [String] -> [Expression] -> [(String, Expression)]
helper (a:ar) (b:br) = (a, b) : helper ar br 

fromBool (Bool b) = b
--48
main = do
  let m = "m"
      k = Variable "k"  -- parse error on input `='
      y = Variable "y"
      lit m = Lit (Num m)
      a0 = Func [“k”] (Prim Mul [k, lit 2))
      in return $ Let [(“y”, a0)] (Prim Add [Call y [lit 3], Call y [lit 10]]) 
-- This is basically Let y = func(k) k*2 in y(3) + y(10) which returns 26.

1 个答案:

答案 0 :(得分:5)

  • 第40行eval应为evaluate,否则您将使用一些名为evaluate的单独的,无关的函数来分解eval函数的定义。< / p>

  • 第54,55行。您正在使用一些unicode方向引号,但应该使用ASCii "

  • 第54行,当你需要方括号'''时,你正在使用')'。

  • helper的类型不正确,其输入会破坏抽象。为了使其更正,您应该将Expression更改为Value,但为了与您的抽象保持一致,您应该将结果设为Environment

编辑:

正如我所说,我将帮助者的类型签名更改为:

helper :: [String] -> [Value] -> [(String, Value)]

EDIT2:

评论您编辑的代码。你违反了第55条,意外删除了一个左栏:

Let ["y", a0)]  -- should be Let [("y", a0)]

在此之前,您尝试修复了语法错误[ ... )的列表,但您更改了错误的括号,导致( ...)而不是正确的[...]

具体做法是:

[k, lit 2]