[Fwd: Re: type class VS struct/functor]
Ashley Yakeley
ashley@semantic.org
Sat, 19 Jan 2002 15:33:22 -0800
At 2002-01-18 18:59, Ahn Ki-yung wrote:
>Each of these Set types takes a string, but the membership test is
>different. This is an annoying case in Haskell, because you can make a
>String a member of the Eq typeclass in only one way.
Your example seemed to translate into Haskell quite straightforwardly...
module FunctorTest where
{
import Char;
lowercase = fmap toLower;
data EQ' t = EQ'
{
eq :: t -> t -> Bool
};
data SET elt set = SET
{
empty :: set,
add :: elt -> set -> set,
mem :: elt -> set -> Bool
};
fSet :: EQ' t -> SET t [t];
fSet mEq = SET
{
empty = [],
add = \elt set -> elt:set,
mem = let
{
mm elt [] = False;
mm elt (x:xs) = if eq mEq x elt then True else mm elt xs;
} in mm
};
sensitiveCase :: EQ' String;
sensitiveCase = EQ'
{
eq = \s s' -> s == s'
};
insensitiveCase :: EQ' String;
insensitiveCase = EQ'
{
eq = \s s' -> (lowercase s) == (lowercase s')
};
sensitiveSet = fSet(sensitiveCase);
insensitiveSet = fSet(insensitiveCase);
}
--
Ashley Yakeley, Seattle WA