0

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

2 回答 2

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

于 2012-12-22T19:23:15.727 回答
3

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

printStmt (If e s1)
printStmt (While e s)
printStmt (Assign s e)
于 2012-12-22T19:23:43.660 回答