8. Short Exercises A
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 Int
s: 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.
-
Write a data definition for the GridAnim type.
-
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
.
-
Write the
RacketPicture
class. -
Create a
SimplePic
data type that holds Doubles forpixelWidth
andpixelHeight
, and a Picture. -
Make
SimplePic
an instance ofRacketPicture
.
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
- In Item One, does the
mult
function take in B’s or Num’s? - What does Item Three do?
- What is the difference between Item One and Item Three?