[英]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)
被解释为一对(两个元组)。
假设您对printBExpr
和printAExpr
定义都可以,那么您需要删除模式匹配项中的逗号:
printStmt (If e s1)
printStmt (While e s)
printStmt (Assign s e)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.