Skip to content

Instantly share code, notes, and snippets.

@roman
Created November 1, 2009 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman/223732 to your computer and use it in GitHub Desktop.
Save roman/223732 to your computer and use it in GitHub Desktop.
op2str :: Op -> String
op2str Plus = " + "
op2str Minus = " - "
op2str Mul = " * "
op2str Div = " / "
op2str Pow = " ^ "
prettyShow :: (Show a, Num a) => SymbolicManip a -> String
prettyShow = fst . prettyShow'
prettyShow' :: (Show a, Num a) => SymbolicManip a -> (String, Maybe Op)
prettyShow' (Number x) = (show x, Nothing)
prettyShow' (Symbol x) = (x, Nothing)
prettyShow' (UnaryArith op2str a) = (op2str ++ "(" ++ show a ++ ")", Nothing)
prettyShow' (BinaryArith op a b) =
let (ra, mopa) = prettyShow' a
(rb, mopb) = prettyShow' b
opa = maybe op id mopa
opb = maybe op id mopb
pop = op2str op
in if opa == op && opb == op then (ra ++ pop ++ rb, Just op)
else if opa == op && opb /= op then (ra ++ pop ++ "(" ++ rb ++ ")", Just op)
else if opa /= op && opb == op then ("(" ++ ra ++ ")" ++ pop ++ rb, Just op)
else ("(" ++ ra ++ ")" ++ pop ++ "(" ++ rb ++ ")", Just op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment