Haskell - Type error in application: Type does not match

835 views Asked by At

I am getting a matching error:

Expression : parseExpr (append p e) es

Term : parseExpr

Type : Expr -> String

Does not match : 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. Is there a way to state this?

1

There are 1 answers

0
ErikR On

Perhaps your confusion is here:

data Expr = ... | Expr String

This does not mean that every Expr can be converted to a string. It just means that there is a function called Expr which takes a String and returns an Expr (the type).

The obvious way to define parseExpr for the Pred case is to call parseExpr on the elements of the Expr list, e.g.:

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? A concrete example would be helpful.