functions not in type classes

Ashley Yakeley [email protected]
Fri, 18 Jan 2002 00:27:09 -0800


At 2002-01-18 00:06, Cagdas Ozgenc wrote:

>Why does Haskell let you write functions that are not a part of type class?

Well, what classes should such functions as const, id and (.) be members 
of?

const :: a -> b -> a;
const x y = x;

id :: a -> a;
id x = x;

(.) :: (b -> c) -> (a -> b) -> (a -> c);
(.) f g x = f (g x);

>For example instead of
>
>elem :: Eq a => a -> [a] -> Bool
>map :: (a -> b) -> [a] -> [b]
>
>class Container a where
>    elem :: Eq b => b -> a b -> Bool
>    map :: (b -> c) -> a b -> a c
>
>would define methods that can work on all containers, 

Actually this would only work on container type-constructors. If you had 
a type that was some kind of container only of Char, you could not make 
it an instance of your Container.

>and create a discipline to write more reusable and generic functions.

There's already a generalised version of map, called fmap, that works on 
any Functor type. Picking the correct generalisation is often not 
obvious...

-- 
Ashley Yakeley, Seattle WA