Skip to content

Instantly share code, notes, and snippets.

@d8660091
Last active March 22, 2016 20:25
Show Gist options
  • Save d8660091/f8e84434067fe4e02f9c to your computer and use it in GitHub Desktop.
Save d8660091/f8e84434067fe4e02f9c to your computer and use it in GitHub Desktop.
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