简体   繁体   中英

Haskell, could not match expected type

I'm new to Haskell and I can't figure out whats going wrong.

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)

Can anybody please tell me where I get an "could not match expected type" error here?

Remove the commas from the patterns:

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) is interpreted as a pair (two-tuple).

Assuming your definitions for printBExpr and printAExpr are ok, you need to remove the commas in your pattern matches:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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