Search code examples
haskellalgebraic-data-types

Unsure how to use a given data type


I'm doing some homework and I have to use the following definition:

data Ponto = Cartesiano Double Double | Polar Double Double deriving (Show,Eq)

to write a function that calculates the distance of a point to the vertical axis, the coordinates can be as x and y or r and angle using the data type above.

Can you help me understand how I should use this type of definition in Haskell?


Solution

  • You just need to define the function piecewise for each data constructor available for the type:

    yourFunction :: Numa a => Ponto -> a
    yourFunction (Cartesiano x y) = ...
    yourFunction (Polar r th) = ...