{-# OPTIONS -fglasgow-exts #-} module Dictionary(main) where import Bench import Data.Collections import Data.Monoid import Data.Trie(Trie) import Data.Typeable import Prelude hiding (sum) import System.Environment allWords 0 = [[]] allWords n = do x <- ['a'..'z'] xs <- allWords (n-1) return (x:xs) count p c = getSum $ foldMap (Sum . i . p) c where i True = 1::Int i False = 0 benchmark :: forall coll a. (Typeable coll, Collection coll a a, Map coll String ()) => coll -> [a] -> Benchmark () benchmark typ words = withLab (show $ typeOf typ) $ do dict <- withLab "fromList" $ time (fromList words :: coll) c <- withLab "member" $ time (count (`member` dict) (allWords 3)) blift $ print c main = do [fileName] <- getArgs file <- readFile fileName let words = lines file putStrLn $ show (length words) ++ " words read" putStrLn $ "totalling " ++ show (sum $ (map length) words) ++ " characters." runBenchmark $ do benchmark (__::StdSet String) words benchmark (__::AvlSet String) words benchmark (__::Trie String Char ()) (zip words (repeat ()))