I have written this data type:
data poly = litt integer. Wise | Shift shift | Original polypoli
I would also like to write a function printPoly for it, which takes a poly expression and converts it to string. This can be implemented only by creating a custom example of the show for the data type: eg Shift:
show example poly where show (lit x) = show x show (above) = "x Show (XIC) + (Show X) ++ "+" ++ (Show Wi) Show (original XI) = (Show X) ++ "*" ++ (Show Y)
Now if I add an expression like <$> (value) $ original (war) $ original Var var add
main = do print (such as $ 1 lite)
< / Ex>gives it
1 + x + x * x * x
. What I want is however, I want this function to be printPoly, like:printoli :: poly-> string print poly (lit x) = show x printoli ( Var = "x" printPoly (add xy) + (show x) ++ "+" ++ (show y) print poly (original xy) = (show x) ++ "*" ++ (show y) < / Code>
The way I wrote this function, where am I wrong?
To answer your question - this is what you want to do.
printoli :: shift - & gt; String Printoli (Lit X) = Show X Print Poly (Var) = "X" printoli (add XI) = (printpole x) ++ "+" ++ (+ printoli wi) Printoli (Mul XY) = (printoli x) ++ See "+" + (Printoli Wai) instance ply, which shows = printoli
However, there are some obvious problems in it. Consider Print (Add Original (Lit 2) $ var Var)
It will print 2 * x + x
, when it is not obvious to follow the priority rules. String Printoli (lit X) = Show X Print Poly (VAR) = "X" printoli (add XI) = (BX $ printpie x) ++ "+" ++ (by $ printpole y) Where B (AD _ _) P = "(" ++ p ++ ")" b _p = p print poly (root xy) = (bx $ printPoly x) ++ "*" ++ (by $ printpole y) where b (Mul _ _) p = "(" ++ p ++ ")" b _p = p
will print 2 * (x + x)
which is given correct Explanation of AST Besides, you can use printf
to Text.Printf
printoli :: poly-> And can simplify the whole thing. String Printoli (Lit X) = Show x PrintPoly (VAR) = "X" Printoli (Add XI) = "% s +% s" (BX $ PrintPiece X) ($ by PrintPoli Y) where B (ADD _ _ ) = Printf "(% s)" b _ = id printoli (original xy) = printf "% s *% s" (bx $ printPoly x) ($ by printPoly y) where b (Mul _ _ =) = printf "( % S) "b _ = id"
Of course, it is still doing many link-lists, translating strings together so it will not be too fast, but at least Less this Is only;)
Comments
Post a Comment