[Haskell-beginners] very impure [global] counter

Patrick LeBoutillier patrick.leboutillier at gmail.com
Fri Jul 22 11:55:16 CEST 2011


Davi,

Perhaps you could use openTempFile
(http://haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/System-IO.html#22)?

It will create a file with a unique name and open it for you, you can
then just close it and pass the filename to your program.

Patrick


On Fri, Jul 22, 2011 at 3:10 AM, Davi Santos <dps.abc at gmail.com> wrote:
> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada



More information about the Beginners mailing list