[Haskell-cafe] Question about data

Thomas DuBuisson thomas.dubuisson at gmail.com
Fri Aug 19 22:45:07 CEST 2011


This is not a valid data declaration.  You can't have a "Float" field
without any constructor name and have it still of type
"MathExpression".  I suggest you do something like:

data MathExpr = MathFloat Float
    ....

So you may declare pi:

    let mathPi = MathFloat pi  -- note "pi" is defined in the prelude alread


Cheers,
Thomas

On Fri, Aug 19, 2011 at 1:40 PM, Paul Reiners <paul.reiners at gmail.com> wrote:
> I've created a simple type declaration:
>
> data MathExpression = Float
>     | Add MathExpression MathExpression
>     | Subtract MathExpression MathExpression
>     | Multiply MathExpression MathExpression
>     | Divide MathExpression MathExpression
>       deriving (Show)
>
> Now how do I create an instance of MathExpression which is just a Float?
>
> This doesn't work:
>
> *Main> let pi = 3.14 :: MathExpression
>
> <interactive>:1:10:
>     No instance for (Fractional MathExpression)
>       arising from the literal `3.14'
>     Possible fix:
>       add an instance declaration for (Fractional MathExpression)
>     In the expression: 3.14 :: MathExpression
>     In an equation for `pi': pi = 3.14 :: MathExpression
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



More information about the Haskell-Cafe mailing list