0

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?

Community
  • 1
  • 1
SalmaFG
  • 2,022
  • 3
  • 23
  • 36
  • 3
    The error message seems clear enough to me. `parseExpr` is of type `Expr -> String`, but you're calling it with two arguments. – kosmikus Nov 22 '14 at 13:03
  • in `parseExpr (Pred p (e:es)) = ...` `p` will be a `String`, `e` will be an `Expr` and `es` will be a `[Expr]`. Why do you think `e` will be a `String`? – ErikR Nov 22 '14 at 13:19
  • @user5402 Because Expr can always be broken down into a String I guess? Isn't that correct? I'm trying to perform a toString for Expr. – SalmaFG Nov 22 '14 at 13:23
  • 1
    still, `parseExpr` takes just one argument. But in `parseExpr (append p e) es` you call it with 2 arguments. That's not going to work... – Michael Nov 22 '14 at 13:30
  • @Michael is there a different way to type cast Expr to a String? – SalmaFG Nov 22 '14 at 13:36
  • @SalmaFG Can you add to your question what you are trying to do? Possible a sample input and output will help the readers of this question. – Sibi Nov 22 '14 at 13:53
  • 1
    Please install `ghc` and uninstall `hugs` forever. – alternative Nov 22 '14 at 15:01

1 Answers1

1

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.

ErikR
  • 51,541
  • 9
  • 73
  • 124