import System import Maybe import Data.HashTable update ht1 ht2 key = do (Just x) <- Data.HashTable.lookup ht1 key maybey <- Data.HashTable.lookup ht2 key case maybey of Just y -> do delete ht2 key; insert ht2 key (x+y) Nothing -> do insert ht2 key x applyEach [] = return () applyEach (x:xs) = x >> applyEach xs main = do [n] <- getArgs let elements = [0..9999] keys = map (\x -> "foo_"++(show x)) elements ht1 <- fromList hashString $ zip keys elements ht2 <- new (==) hashString :: IO (HashTable String Int) -- foldr (<<) $ concat $ replicate (read n) [ update ht1 ht2 key | key <- keys ] -- applyEach [ applyEach $ replicate (read n) $ update ht1 ht2 key | key <- keys ] -- sequence_ $ concat $ replicate (read n) [ update ht1 ht2 key | key <- keys ] sequence_ [ sequence_ $ replicate (read n) $ update ht1 ht2 key | key <- keys ] vals <- sequence [Data.HashTable.lookup ht1 "foo_1" ,Data.HashTable.lookup ht1 "foo_9999" ,Data.HashTable.lookup ht2 "foo_1" ,Data.HashTable.lookup ht2 "foo_9999" ] putStrLn $ unwords $ map (show . fromJust) vals