fork download
  1. {-# LANGUAGE DeriveDataTypeable #-}
  2. import Data.Generics
  3.  
  4. data D1 = D1 Int String deriving (Data, Typeable)
  5. data D2 = D2 D1 Int (Int -> Int) deriving (Data, Typeable)
  6.  
  7. count :: (Data a, Typeable b) => b -> a -> Int
  8. count t = everything (+) (0 `mkQ` c t)
  9. where
  10. c :: a -> a -> Int
  11. c _ _ = 1
  12.  
  13. main = do
  14. let c = undefined :: Char
  15. i = undefined :: Int
  16. ii = undefined :: Int -> Int
  17. s = [c,c,c]
  18. d = D2 (D1 i s) i ii
  19. print . count c $ d
  20. print . count i $ d
  21. print . count ii $ d
  22. print . count s $ d
  23.  
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )
Linking prog ...
stdout
3
2
1
4