[Haskell-beginners] Re: uses for Functor or Applicative?

Heinrich Apfelmus apfelmus at quantentunnel.de
Sat Jul 24 06:44:31 EDT 2010


Johann Bach wrote:
> I'm wondering if the following problem might reveal a use for Functor
> or Applicative, or just generally something cooler than I'm doing now.
> 
> I have a music document as type MusDoc. It has [Part]. Part has
> [Note]. I have defined them using field names because I am
> deliberating allowing for this to get more complicated later.
> 
> The question is about mapping functions over the individual parts. I
> often find myself wanting to alter or
> filter all the parts. In the following example I define filterDoc with
> a couple of helper functions. What I
> would like to know: is there a cooler way to do this using instances
> of Functor or something? Defining
> operators?

You could make the types polymorphic to be able to make a functor 
instance, but it's not necessarily a good idea.


In any case, you may want to have a look at  functional references

   http://article.gmane.org/gmane.comp.lang.haskell.cafe/28094

One implementation of functional references is

   http://hackage.haskell.org/package/data-accessor

Then, you can for instance write

     parts :: Accessor MusDoc [Part]
     notes :: Accessor Part [Notes]

     example :: MusDoc -> MusDoc
     example = modify parts . map . modify notes $ filter (== CSharp)

where  notes  and  parts  are two appropriate functional references, 
which are best generated automatically.


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list