简体   繁体   中英

Haskell Polymorphism

data Set a = Set [a]

-- Example 1
instance (Eq a, Ord a)  => Eq (Set a) where 
  (Set xs) == (Set ys) = (sort xs) == (sort ys) 

-- Example 2 
instance (Eq a, Ord a)  => Eq (Set a) where   
  (==) = eqSet

eqSet (Set xs) (Set ys) = (sort xs) == (sort ys)

Example 1 and 2 are different ways of overriding the '==' for equality testing for the Set datatype. I'd like to know which way is preferable and why?

Thanks.

There is no difference aside from the existence of the eqSet identifier and inlining concerns which can be solved with an {-# INLINE eqSet #-} pragma. My personal preference would be to use the inlined version since the separate definition doesn't provide any value, but I don't think it matters much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM