
Hi all, I've been reading through Okasaki's purely functional data structures and noticed that the source he provides for the Set implementations uses multi-parameter type classes, whereas those for the Heap structures does not. class Set s a where empty :: s a insert :: a -> s a -> s a ... class Heap h where empty :: Ord a => h a insert :: Ord a => a -> h a -> h a ... The only difference I can see is that the instance declarations for Set contain the Ord constraint, whereas they are defined on the class functions for Heap. instance (Ord a) => Set RedBlackSet a where ... I realise that the type of `a` needs to be constrained to Ord instances at some point in the code, but is there any particular reason for doing it differently in each case? If not, should one method be preferred over the other? Thanks for the help, David.

Maybe some instances of Sets can be built with just equality (Eq) rather than Ordering (Ord) on the element? The multi-param class makes the element type tangible so you can use a different constraint: -- List - the poor man's set. instance (Eq a) => Set [] a where empty = [] insert a as = nub (a:as) ...
participants (2)
-
David Beacham
-
Stephen Tetley