Exercise 2.1

Posted in Uncategorized at 10:22 pm by admin

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.


  1. The Haskell School of Expression: Exercises » Exercise 2.2 said,

    July 24, 2007 at 11:05 pm

    […] Note that the vertex list is in anticlockwise order. This is also the case with Exercise 2.1. […]

  2. Howard Lewis Ship said,

    October 19, 2007 at 12:57 pm

    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

  3. Giarome said,

    October 24, 2009 at 4:35 pm

    In my point of view, it is clockwise!

  4. Derek Mahar said,

    August 26, 2011 at 4:04 pm

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

  5. Joshua Nahum said,

    December 19, 2011 at 1:01 pm

    Howard is correct, “rectangle :: Side -> Side -> Shape” matches the data definition, as Rectangle doesn’t take a tuple.

  6. linder said,

    January 19, 2012 at 12:15 pm

    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.

  7. Doug said,

    October 20, 2014 at 6:28 pm

    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 Comment