Christopher Howard | 14 May 00:08 2013

fromIntegral not enough?

This is probably a haskell-beginners sort of question, but I usually get
about 4x as many responses from cafe, about 10x as fast.

I have code like so:

code:
--------
data Xy a = Xy a a

class Coord2 a where

  coords2 :: Fractional b => a b -> Xy b

data CircAppr a b = CircAppr a b b -- number of points, rotation angle,
radius
    deriving (Show)

instance Integral a => Coord2 (CircAppr a) where

  coords2 (CircAppr divns ang rad) =
      let dAng = 2 * pi / (fromIntegral divns) in
      let angles = map (* dAng) [0..divns] in
      undefined -- To be coded...
--------

In the instance definition divns is an integral trying to divide a
fractional. I hoped wrapping it in fromIntegral would coerce, but
apparently not:

quote:
(Continue reading)

Tom Ellis | 14 May 00:43 2013
Picon

Re: fromIntegral not enough?

On Mon, May 13, 2013 at 02:08:26PM -0800, Christopher Howard wrote:
> instance Integral a => Coord2 (CircAppr a) where
> 
>   coords2 (CircAppr divns ang rad) =
>       let dAng = 2 * pi / (fromIntegral divns) in
>       let angles = map (* dAng) [0..divns] in
>       undefined -- To be coded...

Your definition of "angles" forces "dAng" to be of type "a".  Then in order
to define "dAng" as the result of a "/" there must be a "Fractional" instance for
"a".

Hope that helps.

Tom
Tom Ellis | 14 May 00:53 2013
Picon

Re: fromIntegral not enough?

On Mon, May 13, 2013 at 11:43:41PM +0100, Tom Ellis wrote:
> On Mon, May 13, 2013 at 02:08:26PM -0800, Christopher Howard wrote:
> > instance Integral a => Coord2 (CircAppr a) where
> > 
> >   coords2 (CircAppr divns ang rad) =
> >       let dAng = 2 * pi / (fromIntegral divns) in
> >       let angles = map (* dAng) [0..divns] in
> >       undefined -- To be coded...
> 
> Your definition of "angles" forces "dAng" to be of type "a".  Then in order
> to define "dAng" as the result of a "/" there must be a "Fractional" instance for
> "a".

You probably want

    let angles = map ((* dAng) . fromInteger) [0..divns] in
    ...

instead.
Christopher Howard | 14 May 02:07 2013

Re: fromIntegral not enough?

On 05/13/2013 02:53 PM, Tom Ellis wrote:
> On Mon, May 13, 2013 at 11:43:41PM +0100, Tom Ellis wrote:
> 
> You probably want
> 
>     let angles = map ((* dAng) . fromInteger) [0..divns] in
>     ...
> 
> instead.
> 

Ah, that works. Thanks all.

--

-- 
frigidcode.com

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane