繁体   English   中英

Haskell,无法匹配预期的类型

[英]Haskell, could not match expected type

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

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

从模式中删除逗号:

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

假设您对printBExprprintAExpr定义都可以,那么您需要删除模式匹配项中的逗号:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM