Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type error in Haskell

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?

like image 474
Paul Reiners Avatar asked Feb 18 '26 21:02

Paul Reiners


2 Answers

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.

like image 159
fuz Avatar answered Feb 21 '26 14:02

fuz


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)

like image 41
Random Dev Avatar answered Feb 21 '26 14:02

Random Dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!