Posted By

eagletmt on 09/15/09


Tagged


Versions (?)

element


 / Published in: Haskell
 

  1. {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, OverlappingInstances #-}
  2. import Data.Foldable (Foldable)
  3. import qualified Data.Foldable as F
  4.  
  5. class Elem e c where
  6. element :: e -> c -> Bool
  7. instance (Eq e, Foldable t) => Elem e (t e) where
  8. element = F.elem
  9. instance (Elem e c, Foldable t) => Elem e (t c) where
  10. element e = F.any (element e)
  11.  
  12. main = do
  13. print $ (element :: Int -> [Int] -> Bool) 4 [1,2,3] -- False
  14. print $ (element :: Int -> [[Int]] -> Bool) 4 [[1],[2,4],[3]] -- True
  15. print $ (element :: [Int] -> [[Int]] -> Bool) [2] [[1],[2,4],[3]] -- False

Report this snippet  

You need to login to post a comment.