Guest User

Untitled

a guest
Apr 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. op2str :: Op -> String
  2. op2str Plus = " + "
  3. op2str Minus = " - "
  4. op2str Mul = " * "
  5. op2str Div = " / "
  6. op2str Pow = " ^ "
  7.  
  8. prettyShow :: (Show a, Num a) => SymbolicManip a -> String
  9. prettyShow = fst . prettyShow'
  10.  
  11. prettyShow' :: (Show a, Num a) => SymbolicManip a -> (String, Maybe Op)
  12. prettyShow' (Number x) = (show x, Nothing)
  13. prettyShow' (Symbol x) = (x, Nothing)
  14. prettyShow' (UnaryArith op2str a) = (op2str ++ "(" ++ show a ++ ")", Nothing)
  15. prettyShow' (BinaryArith op a b) =
  16. let (ra, mopa) = prettyShow' a
  17. (rb, mopb) = prettyShow' b
  18. opa = maybe op id mopa
  19. opb = maybe op id mopb
  20. pop = op2str op
  21. in if opa == op && opb == op then (ra ++ pop ++ rb, Just op)
  22. else if opa == op && opb /= op then (ra ++ pop ++ "(" ++ rb ++ ")", Just op)
  23. else if opa /= op && opb == op then ("(" ++ ra ++ ")" ++ pop ++ rb, Just op)
  24. else ("(" ++ ra ++ ")" ++ pop ++ "(" ++ rb ++ ")", Just op)
Add Comment
Please, Sign In to add comment