data Expr = Var Char | Tall Int | Sum Expr Expr | Mult Expr Expr | Neg Expr | Let Expr Expr Expr
deriving(Eq, Show)
This is the data type for Expr, I have a few questions. I intend to parse type expressions *(Expr,Expr)as shown in the data type definition. However, I have some problems with "creating" the actual Expr. I use pattern matching to recognize various things that can be Expr. Another code:
parseExpr :: String -> (Expr, String)
parseExpr ('*':'(':x:',':y:')':s) = (Mult (parseExpr [x] parseExpr [y]),s)
This does not work, obviously. The return type parseExprshould return the rest of the expression, which should be parsed as part of the parsed code Expr. The right side of this code is a problem. I can not make valid Expr. A function should call it recursive until the problem is resolved.
Another problem is that I do not know how to match patterns with Varand Tall. How can I check what Varis an uppercase character between AZ and what Tallis 0-9 and returns it as valid Expr?
In general, I can just take a look at several parts of the line to understand which part ExprI am dealing with.
Input like: parseProg "let X be 9 in *(X , 2)" Would spit out: Let (Var 'X') (Tall 9) (Mult (Var 'X') (Tall 2))
source
share