[Haskell-beginners] uses for Functor or Applicative?
Stephen Tetley
stephen.tetley at gmail.com
Fri Jul 23 06:57:00 EDT 2010
Hi Johann
A simple metric to help you decide is to ask the question "Do I need
type changing?" as that's somewhat the essence of the Functor class.
It doesn't look like any of you types are parametric - so thus far you
don't need type changing.
You could open up your datatypes making them parametric on maybe Note
or Pitch. For instance you might decide you want two representations
of Pitch:
a) Integer for generating MIDI,
b) Symbolic for printing - Pitch letters plus octave - C | Cs | Db | D ...
As MusicDoc, Part etc must be made parametric to accommodate this, it
would be obvious to make them instances of Functor and the related
classes Traversable and Foldable which give you nice operations for
generic traversals.
But if then you want to be parametric on Duration as well, then you
have a problem - there aren't common classes for BiFunctor,
BiFoldable, BiTraversable... I've represented music myself with a
TriFunctor - parametric on Pitch, Duration and Annotation - in
practice it wasn't a good way to do it.
The other alternative is to use a "generics" library - currently
Uniplate is considered the simplest. Generics libraries provide
generic traversals of data structures without the restriction to
Functor types.
Best wishes
Stephen
More information about the Beginners
mailing list