我正在使用树结构,并且有以下几行代码,但是我不明白是什么原因引起了麻烦。我的reshapeTree函数将AST和Mapping作为参数并返回AST。我真的不知道是什么原因导致此错误,如果能提供帮助,将不胜感激。谢谢
我收到以下错误,代码如下:
test1.hs:55:35: error: * Couldn't match type[Char]' with
AST' Expected type: [(String, AST, String)] Actual type: Mapping * In the second argument ofreshapeTree', namely
mapping' In the expression: reshapeTree tree mapping In an equation for `newTree': newTree = reshapeTree tree mapping | 55 | where newTree = reshapeTree tree mapping | ^^^^^^^type Mapping = [(String, String, String)] data AST = EmptyAST | ASTNode String AST AST deriving (Show, Read) reshapeTree (EmptyAST) (_) = EmptyAST reshapeTree (ASTNode val l r) [] = (ASTNode val l r) reshapeTree (ASTNode val l r) [(v1,v2,v3)] |val == v1 = (ASTNode v3 v2 EmptyAST) |otherwise = (ASTNode val (reshapeTree l [(v1,v2,v3)]) (reshapeTree r [(v1,v2,v3)])) reshapeTree (ASTNode val l r) ((v1,v2,v3):xs) |val == v1 = (ASTNode v3 v2 EmptyAST) |otherwise = reshapeTree (ASTNode val (reshapeTree l ((v1,v2,v3):xs)) (reshapeTree r ((v1,v2,v3):xs))) xs evalTree (ASTNode val l r) |val == "plus" = show (read (evalTree l) + read (evalTree r) ) |val == "times" = show (read (evalTree l) * read (evalTree r) ) |val == "cat" = (evalTree l) ++ (evalTree r) |val == "negate" = show ((-1) * read (evalTree l)) |val == "len" = show (length(evalTree l)) |val == "str" = evalTree l |val == "num" = evalTree r |otherwise = val evaluateAST (tree,mapping) |mapping == [] = (tree,evalTree tree) |otherwise = (tree, evalTree newTree) where newTree = reshapeTree tree mapping