8. Short Exercises A
Part I
CodeWorld uses Double instead of Float.
Point is an alias for (Double, Double) in CodeWorld.
A grid animation includes two Int: 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. (This problem requires a picture or it is too hard to understand.)
gridcenters :: GridAnim -> [Point]Example:
gridcenters ex2 == [(0,0), (2,0), (4,0), (6,0), (8,0), (0,2), (2,2), (4,2), (6,2), (8,2), etc.]
Part II
RacketPicture is a class that includes functions to measure
imageWidth and imageHeight of an object, both returning Double.
-
Write the
RacketPictureclass. -
Create a
SimplePicdata type that holds Doubles forpixelWidthandpixelHeight, and a Picture. -
Make
SimplePican 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
multfunction take in B’s or Num’s? - What does Item Three do?
- What is the difference between Item One and Item Three?