Exercise 5.1

Recall the definitions from Exercise 2.3.

area' :: Shape -> Float
area' (Polygon (v1:vs)) = let arealist = map (\(v2, v3) -> triArea v1 v2 v3)
                                         (zip vs (tail vs))
                          in foldl (+) 0 arealist

(zip vs (tail vs)) turns out to be a handy idiom.

One Response to “Exercise 5.1”

  1. With foldr
    area” (Polygon (v1:vs)) = foldr (\(v2,v3) ->(+) ( triArea v1 v2 v3)) 0.0 (zip vs (tail vs))

Leave a Reply