Created
November 1, 2009 21:23
-
-
Save roman/223732 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
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