Haskell recursive problem, small parser. Denial of an Expr expression and expression

data Expr =  Var Char | Tall Int | Sum Expr Expr | Mult Expr Expr | Neg Expr | Let Expr Expr Expr
    deriving(Eq, Show) 

parseExpr :: String -> (Expr, String)

parseExpr ('*':'(':s) = (Mult x y, s'')
    where (x,',':s') = parseExpr s
          (y,')':s'') = parseExpr s'
parseExpr ('+':'(':s) = (Sum x y, s'')
    where (x,',':s') = parseExpr s
          (y,')':s'') = parseExpr s'
parseExpr (x : xs) | isDigit x = (Tall (digitToInt x), xs)
parseExpr (x:s) = (Var x,s) 
    where   x >= 'A' = True
        x <= 'Z' = True 

My parser is missing two things before it is complete. Both of the data type above its absence are "Neg Expr" and Let "Expr Expr Expr". The first part will be something like this:

parseExpr('-' 
parseExpr('l':'e':'t':x:'b':'e 

As with the data type, Let expressions begin with let and take three Expr. I do not know how to write these last functions. Any help whatsoever would be greatly appreciated.

I asked here another question about this, and here is a link to this question.


Here is an example:

parseProg "let X be 4 in let Y be *(2 , X) in let Z be +(Y , X) in
+(+(X , Y) , Z)"
Let (Var 'X') (Tall 4) (Let (Var 'Y') (Mult (Tall 2) (Var 'X')) (Let
(Var 'Z') (Sum (Var 'Y') (Var 'X')) (Sum (Sum (Var 'X') (Var 'Y')) (Var
'Z'))))
+3
source share
1 answer

, , , . let :

parseExpr ('l':'e':'t':s) = (Let x y z, s3)
    where (x, 'b':'e':s1) = parseExpr s
          (y, 'i':'n':s2) = parseExpr s1
          (z, s3)         = parseExpr s2

l, e, t, (s) . (x) . , b, e. , (s2) parseExpr . .

, , , "let" "be" . / parseExpr, , :

    where (x, ' ':'b':'e':' ':s1) = parseExpr s

dropSpaces :: String -> String, .

+1

Source: https://habr.com/ru/post/1719368/


All Articles