Exercise 2.1


data Shape = ...
           | Polygon [Vertex]
           ...
type Vertex = (Float, Float)

rectangle :: Float -> Float -> Shape
rectangle s1 s2 = Polygon [(x, y),  (-x, y), (-x, -y), (x, -y)]
                  where x = s1 / 2
                        y = s2 / 2
rtTriangle :: Float -> Float -> Shape
rtTriangle s1 s2 = Polygon [(0, 0),  (s1, 0), (0, s2)]

My rectangle is centred on the origin, with vertices in anticlockwise order, to match up with what’s coming in future chapters and exercises.


7 responses to “Exercise 2.1”

  1. My solution was similar (didn’t bother with centering on origin), however I declared the parameter types (is that the right term in Haskell?) as Side, not Float … i.e. rectangle :: Side -> Side -> Shape

  2. Howard, I don’t think “rectangle :: Side -> Side -> Shape” is correct because Vertex is defined as “(Float, Float)”.

  3. But Rectangle is not the constructor that the exercise wants you to use. The exercise asks you to make a rectangle using the Polygon constructor, which takes a list of vertices. You could make a rectangle with two vertices if they are diagonal. Perhaps it would be helpful to reproduce the exercise that is being solved, to avoid confusion.

  4. The definition of a rectangle does not require that one side be parallel to the y axis. Wikipedia : In Euclidean plane geometry, a rectangle is any quadrilateral with four right angles. The text does not seem to consider rotated shapes as belonging to the Shape type.

Leave a Reply

Your email address will not be published. Required fields are marked *