Collecting values from Functors?

Graham Klyne GK@ninebynine.org
Thu, 05 Jun 2003 15:35:48 +0100


At 20:40 04/06/03 +0200, Tomasz Zielonka wrote:
> > Or a variant of Functor constructor class that I have proposed some time
> > ago on comp.lang.functional:
> >
> > class FunctorM t where
> >     fmapM :: Monad m => (a -> m b) -> (t a -> m (t b))
> >     fmapM_ :: Monad m => (a -> m b) -> (t a -> m ())
> >     fmapM_ f t = fmapM f t >> return ()
> >
> > instance FunctorM [] where
> >     fmapM = mapM
> >     fmapM_ = mapM_
>
>I am sorry, I misunderstood the problem.

I think you responded usefully to the spirit of my question, if not the 
letter.  I think I may be able to use these ideas.

Your FunctorM class looks rather like the kind of idea I was wrestling with 
when I posted my question.

If I define a monad to accumulate the desired result, and a monadic form of 
the transformation/collector function, then I think I can define and use 
fmapM to return a transformed expression (tree, or whatever) and pick the 
accumulated value from the monad.

I'm trying to decide whether to try this (which has the advantage, AFAICT, 
of not using any language extension) or go for the a gmap based approach 
(which may, in the longer run, be more flexible).  Mainly, I'm trying to 
figure which is easier in the near term.

#g


-------------------
Graham Klyne
<GK@NineByNine.org>
PGP: 0FAA 69FF C083 000B A2E9  A131 01B9 1C7A DBCA CB5E