roman (owner)

Revisions

gist: 223732 Download_button fork
public
Public Clone URL: git://gist.github.com/223732.git
Embed All Files: show embed
haskell_numeric_exercise.hs #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)