Quantcast
Channel: How to write an expression over a data type and print it out in haskell? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to write an expression over a data type and print it out in haskell?

$
0
0

I am having to write an expression to construct a Poly representation of the polynomial x^3 + x + 1.

My algebraic data type Poly, which I wrote is:

data Poly = Lit Integer |            Var |            Add Poly Poly |            Mul Poly Poly

The expression I can think of is this, but how can I be able to print out the result using print()?:

expr::Poly->Polyexpr = Add (Lit 1) $ Add (Var) $ Mul (Var) $ Mul Var Var

Also, I'd like to write a function like this:

showPoly::Poly->StringshowPoly (Lit x)   = show xshowPoly (Var)     = "x"showPoly (Add x y) = (show x) ++"+"++ (show y)showPoly (Mul x y) = (show x) ++"*"++ (show y)

to enable the passing of a Poly expression and then converting it to a String. However, the above work tells me that I have no instance for (show Poly), which I am not sure what it mean.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>