2012-12-22 87 views
0

我是Haskell的新手,我无法弄清楚什么是错误的。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) 

有谁可以请告诉我在哪里我得到一个“无法匹配预期类型”的错误在这里?

回答

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)被解释作为一对(双元组)。

+0

非常感谢你! – matg

3

假设你为printBExprprintAExpr定义都OK,你需要删除你的模式匹配的逗号:

printStmt (If e s1) 
printStmt (While e s) 
printStmt (Assign s e) 
相关问题