Haskell,无法匹配预期的类型

时间:2012-12-22 19:16:31

标签: haskell types match

我是Haskell的新手,我无法弄清楚出了什么问题。

data Stmt = If BExpr Stmt
      | While BExpr Stmt
      | Assign String AExpr
      deriving (Eq, Show)
printStmt :: Stmt -> String
printStmt (If e, s1)                = "if " ++ (printBExpr e) ++ " {" ++ (printStmt s1) ++ " }"
printStmt (While e, s)              = "while" ++ (printBExpr e) ++ "{" ++ (printStmt s) ++ "}"
printStmt (Assign s, e)             = s ++ ":=" ++ (printAExpr e)

有人可以告诉我,我在哪里得到“无法匹配的预期类型”错误吗?

2 个答案:

答案 0 :(得分:4)

从模式中删除逗号:

printStmt :: Stmt -> String
printStmt (If e s1)         = "if " ++ (printBExpr e) ++ " {" ++ (printStmt s1) ++ " }"
printStmt (While e s)       = "while" ++ (printBExpr e) ++ "{" ++ (printStmt s) ++ "}"
printStmt (Assign s e)      = s ++ ":=" ++ (printAExpr e)

(If e, s1)被解释为一对(两元组)。

答案 1 :(得分:3)

假设您对printBExprprintAExpr的定义没问题,则需要删除模式匹配中的逗号:

printStmt (If e s1)
printStmt (While e s)
printStmt (Assign s e)