How can I make a counter without Monad?

Tomasz Zielonka tomasz.zielonka at
Wed Mar 16 01:57:13 EST 2005

On Wed, Mar 16, 2005 at 01:17:51AM +0100, Nicolas Oury wrote:
> Greetings,
> In a program, I want to give a unique name to some structures.
> As it is the only imperative  thing I need, I don't want to use a monad.

You don't want to use the IO monad. Why not use some other monad?

> I have played with two solutions and have some questions :
> * unsafePerformIO :

This is asking for trouble. You are using an IO monad in an unsafe way.
Don't do it.

> * linear implicit parameters
> [...]
> Are there other ways to implement a counter in Haskell?

Using a State monad?

>From some of my code:

    let enumeratedTree =
            (`evalState` (0::Int)) $ (`mapTreeM` t) $ 
		    \x -> do n <- next
			     return (n, x)
	next = do a <- get; put $! succ a; return a


    mapTreeM :: Monad m => (a -> m b) -> Tree a -> m (Tree b)
    mapTreeM f (Node a ts) = do
        b <- f a
        ts' <- mapM (mapTreeM f) ts
        return (Node b ts')

(which could also be an instance of a popular non-standard FunctorM

Best regards

More information about the Glasgow-haskell-users mailing list