Advertisement
DanielDv99

big haskell code

Dec 6th, 2020 (edited)
1,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type Variable = String
  2. data Expr
  3.     = Var   Variable
  4.     | Const Int
  5.     | Plus  Expr Expr
  6.     | Mult  Expr Expr
  7.     | Power Expr Expr
  8.     deriving (Eq)
  9.  
  10. instance Show Expr where
  11.     show (Var x) = x
  12.     show (Const c) = show c
  13.     show (Plus e1 e2) = "(" ++ (show e1) ++ " + " ++ (show e2) ++ ")"
  14.     show (Mult e1 e2) = (show e1) ++ "*" ++ (show e2)
  15.     show (Power e1 e2) = (show e1) ++ "^" ++ (show e2)
  16.  
  17. simplifyExpression :: Expr -> Expr
  18. simplifyExpression expr =
  19.     case expr of
  20.         (Var x)       -> Var x
  21.         (Const c)     -> Const c
  22.         (Plus e1 e2)  -> performSum (simplifyExpression e1) (simplifyExpression e2)
  23.         (Mult e1 e2)  -> performMult (simplifyExpression e1) (simplifyExpression e2)
  24.         (Power e1 e2) -> performPower (simplifyExpression e1) (simplifyExpression e2)
  25.  
  26.     where
  27.         performSum (Const c1) (Const c2) = Const (c1 + c2)
  28.         performSum (Const 0) e = e
  29.         performSum e (Const 0) = e
  30.         performSum (Var x) (Var y) =
  31.             if x == y
  32.             then Mult (Const 2) (Var x)
  33.             else Plus (Var x) (Var y)  
  34.         performSum (Plus e1 e2) e3 = performSum e1 (performSum e2 e3)
  35.         performSum e1 e2 = Plus e2 e1
  36.  
  37.         performMult (Const c1) (Const c2) = Const (c1 * c2)
  38.         performMult (Const 0) _ = Const 0
  39.         performMult _ (Const 0) = Const 0
  40.         performMult (Const 1) e = e
  41.         performMult e (Const 1) = e
  42.         performMult (Var x) (Var y) =
  43.             if x == y
  44.             then Power (Var x) (Const 2)
  45.             else Mult (Var x) (Var y)
  46.         performMult (Plus e1 e2) ex = simplifyExpression $
  47.             Plus (Mult e1 ex) (Mult e2 ex)
  48.         performMult ex (Plus e1 e2) = performMult (Plus e1 e2) ex
  49.         performMult (Mult e1 e2) e3 = simplifyExpression $ Mult e1 (Mult e2 e3)
  50.         performMult e1 (Power e2 p) =
  51.             if e1 == e2
  52.             then simplifyExpression (Power e1 (Plus p (Const 1)))
  53.             else Mult e1 (Power e2 p)
  54.         performMult e1 e2 = Mult e2 e1
  55.  
  56.         performPower (Const c1) (Const c2) = Const (c1^c2)
  57.         performPower e1 (Const 1) = e1
  58.         performPower _ (Const 0) = Const 1
  59.         performPower e1 e2 = Power e1 e2
  60.  
  61. diff :: Variable -> Expr -> Expr
  62. diff var (Var v) = if v == var then Const 1 else Const 0
  63. diff _ (Const _) = Const 0
  64. diff var (Plus expr1 expr2)  = simplifyExpression $ Plus (diff var expr1) (diff var expr2)
  65. diff var (Mult expr1 expr2)  = simplifyExpression $ Plus (Mult (diff var expr1) expr2) (Mult expr1 (diff var expr2))
  66. diff var (Power expr1 expr2) = simplifyExpression $ Power newBase newPower
  67.     where
  68.         newBase = Mult (Mult expr1 expr2) (diff var expr1)
  69.         newPower = Plus expr2 (Const (-1))
  70.    
  71. subsitute :: [(String, Int)] -> Expr -> Expr
  72. subsitute dict (Var x) =
  73.     case lookup x dict of
  74.         Just c -> Const c
  75.         Nothing -> Var x
  76. subsitute _ (Const c) = Const c
  77. subsitute dict (Plus e1 e2) = simplifyExpression $ Plus (subsitute dict e1) (subsitute dict e2)
  78. subsitute dict (Mult e1 e2) = simplifyExpression $ Mult (subsitute dict e1) (subsitute dict e2)
  79. subsitute dict (Power e1 e2) = simplifyExpression $ Power (subsitute dict e1) (subsitute dict e2)
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement