You can also modify you class if your intent is that it always work with polymorph types (And it will save you some trouble with functional dependencies between types x and y. Plus, it will be Haskell98 compliant)
-- insert, remove and whatsIn are then supposed to work forall y, and then impose the kind of x to be * -> * (x is a type constructor, not a mere type)
class Container x where
insert :: y -> x y -> x y
remove :: y -> x y -> x y
whatsIn :: x y -> [y]
Then you can do:
import Data.List (delete)
instance Container [] where
insert = (:)
remove = delete -- removes only the first occurence
-- remove x = filter (/= x) -- if you want to remove every occurence of x (there may be a better way)
whatsIn = id
2011/6/10 Patrick Browne
<patrick.browne@dit.ie>
Hi
Below is a class that I wish to create some instances of.
I do not wish to change the class definition.
It is supposed to represent containers of type x that contain things of
type y.
My attempt at the insert function seems ok for Char and lists, but not
ok for Integer. How do I instantiate this class for integers and lists?
Does this class definition permit y to be a list and hence x to be a
list of lists? Something like: instance Containers [] [] where ..
Is this a constructor class?
Thanks,
Pat
class Containers x y where
insert :: y -> x y -> x y
remove :: y -> x y -> x y
whatsIn :: x y -> [y]
instance Containers [] Char where
insert y [] = y:[]
insert y m = y:m
instance Containers [] Integer where
insert y [] = y:[]
insert y m = y:m
-- OK
-- insert '1' ['u','u','l','i']
-- Not OK
-- insert 2 [9,2]
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe