Skip to content

Instantly share code, notes, and snippets.

@keichi
Created June 8, 2011 12:58
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 keichi/1014369 to your computer and use it in GitHub Desktop.
Save keichi/1014369 to your computer and use it in GitHub Desktop.
Show Expression Tree with parenthesis
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