I have a data type called Praat
. I want Praat
to be an instance of Eq
so that two Praat
s are equal if and only if mx
are equal. How does one do this?
-- data type
data Praat t = Praat [k] [(k,k,k,k)]
-- praat gives the maximum frequency
Praat t -> Int
mx (Praat [] _) = 0
mx (Praat (e:es) pt) = ...........
This is how I am trying to define the instance but it is not working.
-- I want to make Praat instance of Eq so that two Praat are equal
-- when their respective `mx` are equal
instance Eq Praat where
mx :: (Praat k)->Int
(mx k) == (mx k) = True
_ == _ = False
class Eq a where Source # The Eq class defines equality ( == ) and inequality ( /= ). All the basic datatypes exported by the Prelude are instances of Eq , and Eq may be derived for any datatype whose constituents are also instances of Eq . The Haskell Report defines no laws for Eq .
An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.
The Haskell standard data type Maybe is typically declared as: data Maybe a = Just a | Nothing. What this means is that the type Maybe has one type variable, represented by the a and two constructors Just and Nothing. (Note that Haskell requires type names and constructor names to begin with an uppercase letter).
instance Eq Praat where
x == y = mx x == mx y
This is pretty much a direct translation of what you said. x
is equal to y
when mx x == mx y
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With