Reputation: 7894
I am getting the following error:
exercise-2-2.hs:15:49:
Couldn't match expected type `Double' with actual type `Int'
In the fourth argument of `regularPolygonHelper', namely `theta'
In the expression: regularPolygonHelper n s 0 theta r
In an equation for `regularPolygon':
regularPolygon n s
= regularPolygonHelper n s 0 theta r
where
r = s / 2.0 * (sin (pi / n))
theta = (2.0 * pi) / n
in the following code:
data Shape = Rectangle Side Side
| Ellipse Radius Radius
| RTTriangle Side Side
| Polygon [Vertex]
deriving Show
type Radius = Float
type Side = Float
type Vertex = (Float, Float)
square s = Rectangle s s
circle r = Ellipse r r
regularPolygon :: Int -> Side -> Shape
regularPolygon n s = regularPolygonHelper n s 0 theta r
where r = s / 2.0 * (sin (pi / n))
theta = (2.0 * pi) / n
regularPolygonHelper :: Int -> Side -> Int -> Double -> Double -> Shape
regularPolygonHelper 0 s i theta r = Polygon []
regularPolygonHelper n s i theta r =
(r * cos (i * theta), r * sin (i * theta)) :
(regularPolygonHelper (n - 1) s (i + 1) theta r)
Why is this? Isn't (2.0 * pi) / n
a double?
Upvotes: 2
Views: 249
Reputation: 52290
better not mix the types so much here is a version that compiles so far:
data Shape = Rectangle Side Side
| Ellipse Radius Radius
| RTTriangle Side Side
| Polygon [Vertex]
deriving Show
type Radius = Double
type Side = Double
type Vertex = (Double, Double)
square s = Rectangle s s
circle r = Ellipse r r
regularPolygon :: Int -> Side -> Shape
regularPolygon n s = regularPolygonHelper n s 0 theta r
where r = s / 2.0 * (sin (pi / fromIntegral n))
theta = (2.0 * pi) / fromIntegral n
regularPolygonHelper :: Int -> Side -> Int -> Double -> Double -> Shape
regularPolygonHelper 0 s i theta r = Polygon []
regularPolygonHelper n s i theta r =
let Polygon rPoly = regularPolygonHelper (n - 1) s (i + 1) theta r in
Polygon ((r * cos (fromIntegral i * theta), r * sin (fromIntegral i * theta)) : rPoly)
Upvotes: 2
Reputation: 93127
Haskell has no automatic conversion between different numeric types. You have to do this by hand. In your case, (2.0 * pi) / fromIntegral n
would do the trick. (You have to add this at all the other places where you want to have a cast too) The reason for this is, that implicit conversion would make type inference much harder, IMHO it is better to have type inference than automatic conversion.
Upvotes: 7