1

so I've looked around for hours and can't find an answer to match what I need, although many are along the same line so I'm sorry if this is somehow a repost.

In haskell, I have this

shapeToPicture :: Shape -> Picture
shapeToPicture (Line p1 p2)         = polyline [p1, p2]
shapeToPicture (Rectangle x y)      = solidRectangle x y
shapeToPicture (Polygon [points])   = solidPolygon [points]
shapeToPicture (Polygon [])         = solidPolygon []
shapeToPicture (Polygon x)          = solidPolygon x
shapeToPicture (Ellipse a b)        = scaled(solidCircle(1), a, b)
shapeToPicture (Circle a b)         = (solidCircle (a, b))

and the Polygon and Line one's will compile, but Rectangle, Circle, and Ellipse will have the error message

src/View.hs:46:57: error:
    • Couldn't match type ‘(Double, Double)’ with ‘Double’
      Expected type: Double
        Actual type: Coords
    • In the second argument of ‘solidRectangle’, namely ‘b’
      In the expression: (solidRectangle a b)
      In an equation for ‘shapeToPicture’:
          shapeToPicture (Rectangle a b) = (solidRectangle a b)
   |
46 | shapeToPicture (Rectangle a b)      = (solidRectangle a b)
   |                                                         ^
Failed, two modules loaded.

I know I have a constructor 'Rectangle' which takes coords, and a codeworld function 'solidRectangle' which takes a Double, but I don't know how to match those so that the code will compile. This message is the same for Circle and Ellipse for the same reason.

note: this is how the constructors are defined

type Coords = (Double, Double)

data Shape
  = Line Coords
         Coords
  | Polygon [Coords]
  | Rectangle Coords
              Coords
  | Circle Coords
           Coords
  | Ellipse Coords
            Coords
  deriving (Show)

Thank you.

CB010101
  • 63
  • 3

2 Answers2

1

As you pointed out, your problem is that solidRectangle takes as second parameter a double and you are passing a tuple of double (Coords) to it.

shapeToPicture (Rectangle x y) = solidRectangle x y -- By data definition y :: (Double, Double)

Depending on what y means in solidRectangle x y you can take either the first or the second of (Double, Double). E.g.

shapeToPicture (Rectangle x y) = solidRectangle x $ fst y
lsmor
  • 4,698
  • 17
  • 38
1

Your Rectangle constructor for Shape has the type Coords -> Coords -> Shape, which is the same as (Double, Double) -> (Double, Double) -> Shape. There are four numbers involved, stored as two pairs of two each. Your pattern match looked like:

shapeToPicture (Rectangle x y)      = solidRectangle x y

If you match up the types, you'll find that x :: Coords and y :: Coords. So x and y are each a pair of numbers, rather than a single number.

This probably isn't what you wanted. You probably meant something like this instead:

shapeToPicture (Rectangle (x1, y1) (x2, y2)) = ...

The pattern matching now breaks apart the pairs, giving you names for the x and y coordinates within each one.

The next question is how to fill in the right-hand side. Unfortunately, this depends on what the Rectangle constructor means, and I don't know. Do you? Did you design the type yourself, or was it given as part of an assignment? If you don't know what it means, then you probably need to figure that out before you can write the right-hand side of this equation.

Chris Smith
  • 2,615
  • 19
  • 18