
Hello, I have just begun reading Hudak's The Haskell School of Expression and i am wondering something about the manner he calculates the area of a polygon (made of vertices) on page 27. He wrote: area(Polygon (v1:vs)) = polyArea vs where polyArea :: [Vertex] -> Float polyArea (v2:v3:vs') = triArea v1 v2 v3 + polyArea(v3:vs') polyArea _ = 0 i do not understand why variable v1 is pattern-matched in the definition of area and made "global" to polyArea instead of doing it with polyArea like this: area(Polygon vs) = polyArea vs where polyArea :: [Vertex] -> Float polyArea (v1:v2:v3:vs') = triArea v1 v2 v3 + polyArea(v3:vs') polyArea _ = 0 Is it an idiom or some sort of optimization ? Or just the way he likes it ? TIA Ludovic Kuty

Ludovic Kuty writes: : | Is it an idiom or some sort of optimization ? It's more to do with the particular algorithm for finding the area of a convex polygon. Try working through the calculation of the area of this kite. Polygon [(0, 0), (1, 0), (2, 2), (0, 1)] I think the two versions of the area function will give different results, and that this will highlight the reason for v1 being matched separately.

At 12:33 13/03/2002 +1300, Tom Pledger wrote:
Ludovic Kuty writes: : | Is it an idiom or some sort of optimization ?
It's more to do with the particular algorithm for finding the area of a convex polygon. Try working through the calculation of the area of this kite.
Polygon [(0, 0), (1, 0), (2, 2), (0, 1)]
I think the two versions of the area function will give different results, and that this will highlight the reason for v1 being matched separately.
That's right. My version is wrong because i forget to take into account the "interior" surface of the polygon. The bigger the polygon, the bigger the difference. Thanks. Ludovic Kuty
participants (2)
-
Ludovic Kuty
-
Tom Pledger