Lab: Frankenshape

Dear Computer

Chapter 9: Types Revisited

Lab: Frankenshape

In today's lab, you'll write a Haskell program that generates a scalable vector graphics (SVG) image out of rectangles and circles. SVG is an XML format that is recognized by most web browsers. You'll define a few types to model the elements of a scene and collect them under a typeclass so you can treat them with a common interface.

Types

Define the following types in file frankenshape.hs:

Svg Typeclass

You want to define a function toSvg for all of the types above. For a function to be eligible for overloading, it must be part of a typeclass. Define the Svg typeclass and have it declare the toSvg function. Then make all three types you defined earlier instances of this typeclass. Fit the signature and definitions to match these example calls and returned strings:

Don't do any output in these functions. They are pure functions that return strings to the caller. Printing is for main.

The Svg typeclass is similar to Show, but it is supported only by types that can produce SVG. Do not make any new instances of the Show typeclass.

Your functions should work for any color, shape, or frankenshape, not just the examples shown.

Main

Write a main function that makes an interesting frankenshape using at least four shapes total and at least one of each variant. Generate the SVG and write it to a file using writeFile. View the file in your browser or some other SVG viewer.

Submit

To receive credit for this lab, you must submit both your frankenshape.hs script and your .svg file on Canvas by Monday noon. Late labs or forgot-to-submits are not accepted because Monday at noon is when your instructor has time to grade.

← Lecture: Transforms