Last active
March 22, 2016 20:25
-
-
Save d8660091/f8e84434067fe4e02f9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prettyShow :: (Show a, Num a) => SymbolicManip a -> String | |
prettyShow val = prettyShowPrec 0 val | |
-- Show a number or symbol as a bare number or serial | |
prettyShowPrec _ (Number x) = show x | |
prettyShowPrec _ (Symbol x) = x | |
prettyShowPrec _ (UnaryArith opstr a) = | |
opstr ++ "(" ++ show a ++ ")" | |
prettyShowPrec outerPrec (BinaryArith op a b) | |
| outerPrec <= innerPrec = pa ++ pop ++ pb | |
| otherwise = "(" ++ pa ++ pop ++ pb ++ ")" where | |
pa = prettyShowPrec innerPrec a | |
pb = prettyShowPrec innerPrec b | |
pop = op2str op | |
innerPrec = getPrec op | |
op2str :: Op -> String | |
op2str Plus = "+" | |
op2str Minus = "-" | |
op2str Mul = "*" | |
op2str Div = "/" | |
op2str Pow = "**" | |
getPrec :: Op -> Int | |
getPrec Plus=5 | |
getPrec Minus=5 | |
getPrec Mul=6 | |
getPrec Div=6 | |
getPrec Pow=7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment