[Haskell-beginners] very impure [global] counter

Davi Santos dps.abc at gmail.com
Fri Jul 22 09:10:55 CEST 2011


Hello all,
I have massive (parallel if possible) system calls to an external
non-deterministic program.
Each time it is executed, it creates a file depending on a command line
option 'opt' (input files path, for example).
How can I ensure the file name will be unique? maybe with a global counter?
My temporary solution have been to use a large random number:

-----------
mysteriousExecution :: String -> IO ()
mysteriousExecution opt = do
   number <- rand
   run $ "mysterious-command " ⊕ opt ⊕ " --create-file=" ⊕ number

rand = do
   a ←  getStdRandom (randomR (1,999999999999999999999999999999999)) ∷  IO
Int
   let r = take 20 $ randomRs ('a','z') (mkStdGen a) ∷  String
   return r
========

I'm trying to avoid additional parameters to 'mysteriousExecution'.
I tried a counter also (to replace rand), but I don't know how could I start
it inside  'mysteriousExecution'.
c ∷  IO Counter
c = do
    r ←  newIORef 0            -- start
    return (do
        modifyIORef r (+1)
        readIORef r)

If somebody says everything is wrong, ok.
I understand. 18 years of imperative programming world can damage the brain.

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110722/e0867294/attachment.htm>


More information about the Beginners mailing list