oneCircle = Shape (Ellipse 1 1)
manyCircles' = [Translate (x,y) oneCircle | x <- centers, y <- centers]
where centers = [0, 2 ..] ++ [-2, -4 ..]
manyCirclesRegion = foldl Union Empty manyCircles'
rectRegion = Translate (4,0) (Shape (Rectangle 10 2))
fiveCircles' = rectRegion `Intersect` manyCirclesRegion
This is technically correct, but the problem here lies with the inability to lazy evaluate Intersect in a matter similar to take. I'm not sure this could easily be made to work: we would need to define Intersect differently and probably also impose an ordering constraint on Region composition. Even then, I think we still have an ordering problem with the positive-and-negative-infinite list evaluation, whereby a bounded Region and an infinite Region (e.g. a semi-infinite halfplane) would be incompatible (although I could be wrong about this depending on how Haskell's lazy evaluation works exactly).
One response to “Exercise 8.1”
As the exercise requested that the five circles have to be of the same size and orientation, I think the use of manyCircles’ is incorrect. Just use the same one defined in the text should be fine.
Using this kind of definition, it runs indefinitely (or until out of memory error) when actually evaluated. For example,
fiveCircles’ `containsR` (0, 0)
I can think of the above short-coming but I don’t know the “trade-offs” the exercise asked about.