简体   繁体   English

Haskell-应用程序中出现类型错误:类型不匹配

[英]Haskell - Type error in application: Type does not match

I am getting a matching error: 我收到匹配错误:

Expression : parseExpr (append pe) es 表达式:parseExpr(追加pe)es

Term : parseExpr 术语:parseExpr

Type : Expr -> String 类型:Expr->字符串

Does not match : a -> b -> c 不匹配:a-> b-> c

when I try executing this code in the marked line: 当我尝试在标记的行中执行以下代码时:

data Expr = Atom String | Var String | Pred String [Expr] | Expr String

append :: String -> String -> String
append a b = a++b

parseExpr :: Expr -> String
parseExpr (Atom a) = a
parseExpr (Var x) = x
parseExpr (Pred p (e:es)) = parseExpr (append p e) es -- ERROR HERE

Although e is always going to be a String from the Expr object definition that I stated above. 尽管e始终是我上面所述的Expr对象定义中的String。 Is there a way to state this? 有没有办法说明这一点?

Perhaps your confusion is here: 也许您的困惑在这里:

data Expr = ... | Expr String

This does not mean that every Expr can be converted to a string. 这并不意味着每个Expr都可以转换为字符串。 It just means that there is a function called Expr which takes a String and returns an Expr (the type). 这仅表示存在一个名为Expr的函数,该函数接受String并返回Expr (类型)。

The obvious way to define parseExpr for the Pred case is to call parseExpr on the elements of the Expr list, eg: Pred案例定义parseExpr的明显方法是在Expr列表的元素上调用parseExpr ,例如:

parseExpr (Pred p exprs) = 
    let strs = map parseExpr exprs  -- this is a [String]
        s    = concat strs          -- this is a String
    in p ++ s

Perhaps you want s = intercalate " " strs to join together the strings with spaces? 也许您希望s = intercalate " " strs将带空格的字符串连接在一起? A concrete example would be helpful. 一个具体的例子会有所帮助。

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

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