Function Overloading in Haskell
haskell, overloading
Solution
In the first case, you want the type of `perpendicular` to be `Line -> Point -> Line`, while in the second case you want it to have the type `Line -> Double -> Line`. This suggests that we can do this with a type class where we abstract over the type of the second argument:
class Perpendicular a where
perpendicular :: Line -> a -> Line
Your first case then becomes an instance for `Point`
instance Perpendicular Point where
perpendicular (Line m b) (Point x y) = Line m2 b2
where m2 = (-1/m)
b2 = y - m2*x
while the second becomes an instance for `Double`.
instance Perpendicular Double where
perpendicular (Line m b) = Line m2
where m2 = (-1/m)
Problem
I have a structure which represents the equation of a line in the form `m x + b` and a structure of a point ``` Line { m :: Double, b :: Double } deriving( Show, Eq ) Point { x :: Double, y :: Double } deriving( Show, Eq ) ``` I want the function `perpendicular` that does the following: ``` perpendicular (Line m b) (Point x y) = Line m2 b2 where m2 = (-1/m) b2 = y - m2*x ``` if given a line and a point, or a partially applied Line ``` perpendicular (Line m b) = Line m2 where m2 = (-1/m) ``` if only given a Line. The problem here is that I get Equations for `perpendicular' have different numbers of arguments