[Haskell-cafe] Question about data

David Barbour dmbarbour at gmail.com
Mon Aug 22 21:53:32 CEST 2011


In addition to fixing 'Float', you should define the 'Fractional' instance
for MathExpression. This would let you use:
   let pi = 3.14 :: MathExpression

So your instance of Fractional would look like:


instance Num MathExpression where
  (+) a b = Add a b
  (*) a b = Multiply a b
  (-) a b = Subtract a b
  negate b = Subtract 0 b
  fromInteger = MathFloat . fromInteger
  abs    = undefined
  signum = undefined
instance Fractional MathExpression where
  fromRational = MathFloat . fromRational
  (/) a b = Divide a b
  recip b = Divide 1 b

That would give 'fromRational' and 'fromInteger' which are the main numeric
conversion functions.

Regards,

Dave

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?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110822/0fa907a2/attachment.htm>


More information about the Haskell-Cafe mailing list