A
A
Anya Kolomenskaya2020-08-15 22:22:02
Haskell
Anya Kolomenskaya, 2020-08-15 22:22:02

How to get rid of parentheses in Haskell?

Here is my Haskell code:

infixl 6 :$:
data ExE  =  ExData Integer | ExE :$: ExE deriving (Show, Eq)

simplify                       :: ExE -> ExE
simplify ((a1 :$: a2) :$: a3)  =  simplify a1 :$: simplify a2 :$: simplify a3
simplify (a1 :$: (a2 :$: a3))  =  simplify a1 :$: simplify a2 :$: simplify a3
simplify (a1 :$: a2)           =  simplify a1 :$: simplify a2
simplify a                     =  a

Here is the result of his work:
*ExTesting> simplify $ (ExData 1 :$: ExData 2) :$: ExData 3
(ExData 1 :$: ExData 2) :$: ExData 3
*ExTesting> simplify $ ExData 1 :$: (ExData 2 :$: ExData 3)
(ExData 1 :$: ExData 2) :$: ExData 3

Then how to get this:
*ExTesting> simplify $ (ExData 1 :$: ExData 2) :$: ExData 3
ExData 1 :$: ExData 2 :$: ExData 3
*ExTesting> simplify $ ExData 1 :$: (ExData 2 :$: ExData 3)
ExData 1 :$: ExData 2 :$: ExData 3

How do I get rid of those extra parentheses in the result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Demin, 2020-08-16
@includedlibrary

You can manually define show for ExE.

instance Show ExE where
  show exe = case exe of
      ExData i -> "ExData " ++ show i
      e1 :$: e2 -> show e1 ++ " :$: " ++ show e2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question