8. Short Exercises A

A bit of data, a bit of record type, a bit of typeclass.

Contents:

  • data, including deriving (Show, Eq, Read, Ord)

Part I

CodeWorld uses Double instead of Float.

Point is an alias for (Double, Double) in CodeWorld.

A grid animation includes two Ints: width and height, a Double for the spacing, and a base Picture. Examples:

ex1 = GridAnim 8 5 2.0 (circle 0.9)
ex2 = GridAnim{width=8, height=5, spacing=2.0, base=(circle 0.9)}

Draw the grid so the upper left base picture is at (0,0) and the spacing is left between the centers of each adjacent image.

  1. Write a data definition for the GridAnim type.

  2. Write a function to determine the centers of each item that will be placed in the grid.

     gridcenter :: GridAnim -> [Point]
    

Part II

RacketPicture is a class that includes functions to measure imageWidth and imageHeight of an object, both returning Double.

  1. Write the RacketPicture class.

  2. Create a SimplePic data type that holds Doubles for pixelWidth and pixelHeight, and a Picture.

  3. Make SimplePic an instance of RacketPicture.

Advanced

Optional.

Setup

Item One:

    class (Num x) => (B x) where
       mult :: x -> x -> x

Item Two:

    class (B x) where
      mult :: x -> x -> x

Item Three:

    instance (Num x) => (B x) where
        mult p q = p - q

Questions

  1. In Item One, does the mult function take in B’s or Num’s?
  2. What does Item Three do?
  3. What is the difference between Item One and Item Three?