I defined a data type:
Data Card = Card Int deriving (Show, Eq)
(I defined also a type synonym:
Type Cards = [Card]
)
and then make it instance of:
instance Ord Card where
x > y |ix == iy = False
|ix == 0 = True
|iy == 0 = False
|otherwise = (ix > iy)
where
ix = label x
iy = label y
Then when I type:
(Card x) > (Card y) :: x,y are Int
it works, but when I type:
[(Card x)] > [(Card y)] :: x,y are Int
it goes in loop.
Why does it happen? How do I fix it?
Your
Ordinstance forCardonly defines>, but you need to define either<=orcompare:The
Ordinstance for lists definescomparein terms of the underlying data type’scompare. By defaultcompareand<=are defined in terms of each other, hence they don’t terminate when called if you have not defined either of them. The otherOrdoperations for list (including>) are defined in terms ofcompare, hence why>doesn’t terminate when you call it on[Card].