Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Variable = String
- data Expr
- = Var Variable
- | Const Int
- | Plus Expr Expr
- | Mult Expr Expr
- | Power Expr Expr
- deriving (Eq)
- instance Show Expr where
- show (Var x) = x
- show (Const c) = show c
- show (Plus e1 e2) = "(" ++ (show e1) ++ " + " ++ (show e2) ++ ")"
- show (Mult e1 e2) = (show e1) ++ "*" ++ (show e2)
- show (Power e1 e2) = (show e1) ++ "^" ++ (show e2)
- simplifyExpression :: Expr -> Expr
- simplifyExpression expr =
- case expr of
- (Var x) -> Var x
- (Const c) -> Const c
- (Plus e1 e2) -> performSum (simplifyExpression e1) (simplifyExpression e2)
- (Mult e1 e2) -> performMult (simplifyExpression e1) (simplifyExpression e2)
- (Power e1 e2) -> performPower (simplifyExpression e1) (simplifyExpression e2)
- where
- performSum (Const c1) (Const c2) = Const (c1 + c2)
- performSum (Const 0) e = e
- performSum e (Const 0) = e
- performSum (Var x) (Var y) =
- if x == y
- then Mult (Const 2) (Var x)
- else Plus (Var x) (Var y)
- performSum (Plus e1 e2) e3 = performSum e1 (performSum e2 e3)
- performSum e1 e2 = Plus e2 e1
- performMult (Const c1) (Const c2) = Const (c1 * c2)
- performMult (Const 0) _ = Const 0
- performMult _ (Const 0) = Const 0
- performMult (Const 1) e = e
- performMult e (Const 1) = e
- performMult (Var x) (Var y) =
- if x == y
- then Power (Var x) (Const 2)
- else Mult (Var x) (Var y)
- performMult (Plus e1 e2) ex = simplifyExpression $
- Plus (Mult e1 ex) (Mult e2 ex)
- performMult ex (Plus e1 e2) = performMult (Plus e1 e2) ex
- performMult (Mult e1 e2) e3 = simplifyExpression $ Mult e1 (Mult e2 e3)
- performMult e1 (Power e2 p) =
- if e1 == e2
- then simplifyExpression (Power e1 (Plus p (Const 1)))
- else Mult e1 (Power e2 p)
- performMult e1 e2 = Mult e2 e1
- performPower (Const c1) (Const c2) = Const (c1^c2)
- performPower e1 (Const 1) = e1
- performPower _ (Const 0) = Const 1
- performPower e1 e2 = Power e1 e2
- diff :: Variable -> Expr -> Expr
- diff var (Var v) = if v == var then Const 1 else Const 0
- diff _ (Const _) = Const 0
- diff var (Plus expr1 expr2) = simplifyExpression $ Plus (diff var expr1) (diff var expr2)
- diff var (Mult expr1 expr2) = simplifyExpression $ Plus (Mult (diff var expr1) expr2) (Mult expr1 (diff var expr2))
- diff var (Power expr1 expr2) = simplifyExpression $ Power newBase newPower
- where
- newBase = Mult (Mult expr1 expr2) (diff var expr1)
- newPower = Plus expr2 (Const (-1))
- subsitute :: [(String, Int)] -> Expr -> Expr
- subsitute dict (Var x) =
- case lookup x dict of
- Just c -> Const c
- Nothing -> Var x
- subsitute _ (Const c) = Const c
- subsitute dict (Plus e1 e2) = simplifyExpression $ Plus (subsitute dict e1) (subsitute dict e2)
- subsitute dict (Mult e1 e2) = simplifyExpression $ Mult (subsitute dict e1) (subsitute dict e2)
- subsitute dict (Power e1 e2) = simplifyExpression $ Power (subsitute dict e1) (subsitute dict e2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement