[Haskell-cafe] New New newbie question/help
Dougal Stanton
ithika at gmail.com
Wed Jun 27 07:45:13 EDT 2007
On 27/06/07, Balu Raman <braman09 at gmail.com> wrote:
> equilateralTri :: Window -> Int -> Int -> Int -> IO()
> equilateralTri w x y side
> = drawInWindow w (withColor Red
> (polygon
> [(x,y),(a,b),(x,y)]))
> where
> b = y + side * sin(pi/3)
> a = x + side * cos(pi/3)
Your problem lies in this section here. Let's look at the error message:
> triangle.hs:17:36:
> No instance for (Floating Int)
> arising from use of 'pi' at triangle.hs:17:36-37
> Probable fix: add an instance declaration for (Floating Int)
> In the first argument of '(/)', namely 'pi'
> In the first argument of 'cos', namely '(pi / 3)'
> In the second argument of '(*)', namely 'cos (pi/3)'
> Failed, modules loaded: none
The problem comes from the calculations of 'a' and 'b'. The function
sin doesn't return an Int value. It returns types within the type
class Floating (annotated as below, for some unspecified 'a').
> sin (pi/3) :: Floating a => a
> side :: Int
Since the type checker has one unknown type, a, and one known, Int, it
tries to put the two together. Then it finds that Int is not an
instance of the Floating class, so a /= Int. So it asks you to make
one:
> Probable fix: add an instance declaration for (Floating Int)
In this case, the advice is bad. There is no reasonable way of making
a machine integer a member of the floating class. What you need to do
instead is ensure that you're using a type that is a member of the
Floating class - that is, convert from an Int before you start the
calculation.
The function fromIntegral should come in handy:
> let n = 3 :: Int
> (fromIntegral n) * sin (pi/3)
2.598076211353316
Good luck!
D.
More information about the Haskell-Cafe
mailing list