41 lines
1.1 KiB
Haskell
41 lines
1.1 KiB
Haskell
{- typeclasses.hs
|
|
|
|
This file contains some of the examples on typeclasses to be shown in
|
|
the second class of the course "Haskell for Life", by Sergiu Ivanov
|
|
(sivanov@lacl.fr):
|
|
|
|
http://lacl.fr/~sivanov/doku.php?id=en:haskell_for_life
|
|
|
|
This file is distributed under the Creative Commons Attribution Alone
|
|
licence.-}
|
|
|
|
-- | A simple redefinition of the 'Eq' typeclass.
|
|
class BasicEq a where
|
|
isEqual :: a -> a -> Bool
|
|
|
|
isElement :: BasicEq a => a -> [a] -> Bool
|
|
isElement _ [] = False
|
|
isElement y (x:xs) | isEqual y x = True
|
|
| otherwise = isElement y xs
|
|
|
|
instance BasicEq Bool where
|
|
isEqual True True = True
|
|
isEqual False False = True
|
|
isEqual _ _ = False
|
|
|
|
instance BasicEq a => BasicEq [a] where
|
|
isEqual [] [] = True
|
|
isEqual _ [] = False
|
|
isEqual [] _ = False
|
|
isEqual (x:xs) (y:ys) | isEqual x y = isEqual xs ys
|
|
| otherwise = False
|
|
|
|
|
|
-- | This typeclasses are simpler than 'Show' and 'Read', but
|
|
-- essentially capture their principal function.
|
|
class SimplifiedShow a where
|
|
show :: a -> String
|
|
|
|
class SimplifiedRead a where
|
|
read :: String -> a
|