Created
June 8, 2011 12:58
-
-
Save keichi/1014369 to your computer and use it in GitHub Desktop.
Show Expression Tree with parenthesis
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
instance Show Expression where | |
show e = showWithParen 0 e where | |
showWithParen _ (Symbol x) = x | |
showWithParen _ (Number x) = show x | |
showWithParen _ (Func name e) = name ++ "(" ++ show e ++ ")" | |
showWithParen w (Op op e1 e2) = head ++ showWithParen curOpWeight e1 ++ show op ++ showWithParen curOpWeight e2 ++ foot where | |
head = if w > curOpWeight then "(" else "" | |
foot = if w > curOpWeight then ")" else "" | |
curOpWeight = getOpWeight op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment