Doing exercises from "Haskell tutorial" ...

It seems like an appropriate page for aske newbie questions, isnt it? I was reading through Haskell tutroial (http://www.di.uminho.pt/afp98/PAPERS/Tutorial.ps) and trying to do exercises from there. I'm stuck in the first exercise for "Classes" chapter (page 11). I need to define SetsAsLists as an instance of Set by supplying definitions for all Set methods, but definitions I wrote led me to adding additional constraints on "union" and "memeber" methods. Is it ok or it's possible to define "union" and "member" without using "==" ? Code follows: class Set s where empty :: s a isEmpty :: s a -> Bool singleton :: a -> s a union :: Eq a => s a -> s a -> s a member :: Eq a => a -> s a -> Bool choice :: s a -> (a, s a) data SetsAsLists a = SL [a] instance Set SetsAsLists where empty = SL [] isEmpty (SL []) = True isEmpty _ = False singleton x = SL [x] member x (SL ys) | isEmpty (SL ys) = False | otherwise = if x==head ys then True else member x (SL (tail ys)) union (SL []) (SL ys) = SL ys union (SL (x:xs)) (SL ys) | member x (SL ys) = union (SL xs) (SL (x:ys)) | otherwise = union (SL xs) (SL ys) -- Dmitry Astapov //ADEpt E-mail: adept@umc.com.ua GPG KeyID/fprint: F5D7639D/CA36 E6C4 815D 434D 0498 2B08 7867 4860 F5D7 639D

Dmitry Astapov wrote (on 02-10-01 15:16 +0300):
It seems like an appropriate page for aske newbie questions, isnt it?
I was reading through Haskell tutroial (http://www.di.uminho.pt/afp98/PAPERS/Tutorial.ps) and trying to do exercises from there. I'm stuck in the first exercise for "Classes" chapter (page 11). I need to define SetsAsLists as an instance of Set by supplying definitions for all Set methods, but definitions I wrote led me to adding additional constraints on "union" and "memeber" methods. Is it ok or it's possible to define "union" and "member" without using "==" ?
It sure looks like an error to me. You definitely need equality on a collection A to define a set over A. I think your implementation is fine. -- Frank Atanassow, Information & Computing Sciences, Utrecht University Padualaan 14, PO Box 80.089, 3508 TB Utrecht, Netherlands Tel +31 (030) 253-3261 Fax +31 (030) 251-379

Dmitry Astapov wrote (on 02-10-01 15:16 +0300):
union (SL []) (SL ys) = SL ys union (SL (x:xs)) (SL ys) | member x (SL ys) = union (SL xs) (SL (x:ys)) | otherwise = union (SL xs) (SL ys)
I take it back: your implementation has a small problem in it, but nothing to do with the Eq class. The two cases in the second clause for union should be switched:
union (SL (x:xs)) (SL ys) | member x (SL ys) = union (SL xs) (SL ys) | otherwise = union (SL xs) (SL (x:ys))
-- Frank Atanassow, Information & Computing Sciences, Utrecht University Padualaan 14, PO Box 80.089, 3508 TB Utrecht, Netherlands Tel +31 (030) 253-3261 Fax +31 (030) 251-379
participants (2)
-
Dmitry Astapov
-
Frank Atanassow