[Haskell-beginners] Question about functors (or maybe not...)

Patrick LeBoutillier patrick.leboutillier at gmail.com
Sat Apr 2 16:57:28 CEST 2011


Felipe,

> You can write your own plain old functions:
>
> mapChord :: (Note -> Note) -> Chord -> Chord
> mapChord f (Chord q n) = Chord q (f n)
>
> mapScale :: (Note -> Note) -> Scale -> Scale
> mapScale f (Scale q n) = Scale q (f n)
>
> You can also create your own type class, if that's convenient for you
>
> class NoteMap a where
>  noteMap :: (Note -> Note) -> a -> a
>
> instance NoteMap Chord where noteMap = mapChord
> instance NoteMap Scale where noteMap = mapScale

Initially I didn't realize how powerful this approach could be, but
once you have noteMap then most of the functions for altering Notes
can then be used on the other types, *without* having to make them
(the functions I mean) be part of the typeclass definition.

This seems obvious but I hadn't really noticed it until now...


Thanks a lot,

Patrick


> HTH, =)
>
> --
> Felipe.
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada



More information about the Beginners mailing list