Collecting values from Functors?
Graham Klyne
gk@ninebynine.org
Thu, 05 Jun 2003 13:18:25 +0100
Ralf,
Thanks. I've just retrieved and read "Scrap your boilerplate" [1]. I must
say that, as a piece of exposition, I think this is an excellent paper
(**). And technically, this is very much the sort of thing I was probing
for. I think I'll focus some attention on gmapM and friends.
(**) for feedback: as a relative newcomer to FP, the part I found most
difficult to track was the section on gfoldl. I found that there was not
quite enough motivation and coverage of its expected properties for me to
get a real handle on. As it is, I'm left with a vague concept that it's a
1-level fold with some fancy type-wrapping. No doubt, in time, I'll gain a
deeper appreciation.
(My original question was intended to strip the question down to bare
bones, separate from details of my desired application, but it may be worth
mentioning some of those details. I have an expression (actually, a form
of graph) that contains "constants" and "variables", and a variable
substitution function. I wish to apply the substitution function across
the graph (of itself a straightforward fmap), and also collect a list of
variables in the graph for which no substitution was provided. All this in
a way that separates details of the expression structure from details of
the variable substitution.)
#g
--
[1] http://www.cs.vu.nl/boilerplate/#paper
At 21:05 04/06/03 +0200, Ralf Laemmel wrote:
>Graham Klyne wrote:
> >
> > I'm trying to figure if there's any way I can use (say) monads to collect
> > values from a Functor.
> >
> > For example, suppose I have a tree of some values that supports fmap, is
> > there any way I can use the fmap function to collect a list of all the node
> > values?
>
>Rather than using fmap, why not use gmap in GHC.
>Using an appropriate generic traversal scheme,
>say listify, all the code you write is the
>following expression:
>
>listify (const True) mytree :: [Int]
>
>if you are looking for all the Ints in
>mytree = Fork (Leaf 42) (Fork (Leaf 88) (Leaf 37))
>So this would give you [42,88,37].
>
>(So you do not need to force your datatypes to become
>functors, neither do you write Functor instances.)
>
>I added this example to the boilerplate page.
>http://www.cs.vu.nl/boilerplate/
>
>Ralf
>
>--
>Ralf Laemmel
>VU & CWI, Amsterdam, The Netherlands
>http://www.cs.vu.nl/~ralf/
>http://www.cwi.nl/~ralf/
-------------------
Graham Klyne
<GK@NineByNine.org>
PGP: 0FAA 69FF C083 000B A2E9 A131 01B9 1C7A DBCA CB5E