[Haskell-cafe] Class and Instance

Patrick Browne patrick.browne at dit.ie
Fri Jun 10 18:28:22 CEST 2011


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



More information about the Haskell-Cafe mailing list