Reputation: 21
I have a custom data type Point Int Int and I need to compare it to another Point Int Int (with elem function). Could you please tell me how to write Eq instance for this data type? Thanks.
type Result = [String]
data Point = Point Int Int
-- missing Eq instance
data Line = Line Point Point
drawField :: [Point] -> (Int, Int) -> Result
drawField positions (width, height) = let
check coordinates | elem coordinates positions = '#'
| otherwise = '.'
in [[check (Point rows cols) | cols <- [0..width-1]] | rows <- [0..height-1]]
Upvotes: 1
Views: 112
Reputation: 476574
If two Point
s are equal if all parameters are elementwise equal, then you can let Haskell derive the Eq
instance with:
data Point = Point Int Int deriving Eq
If the points are equivalent if a more sophisticated test succeeds, you can define your own instance of Eq
with:
instance Eq Point where
Point xa ya == Point xb yb = …
where …
is an expression with Bool
as type.
Upvotes: 8