Another fold question

ajb at spamcop.net ajb at spamcop.net
Wed Nov 5 23:57:10 EST 2003


G'day all.

Quoting Patty Fong <patty_jf at hotmail.com>:

> I also understand how to write my own fold function. What i don't understand
> quite is how to use them. given this data type and this fold function i
> wrote:
> 
> data Music
> 	= Note Pitch Octave Duration
> 	| Silence Duration
> 	| PlayerPar Music Music
> 	| PlayerSeq Music Music
> 	| Tempo (Ratio Int) Music
> data Pitch = Cf | C | Cs
> type Octave = Int
> type Duration = Ratio Int

I know this doesn't answer your question, but for this example, it might
be easier to use some kind of iterator.  In this example:

        getNotes :: Music -> [Music]
        getNotes n@(Note _ _ _) = [n]
        getNotes (PlayerPar m1 m2) = getNotes m1 ++ getNotes m2
        -- etc etc

        count_notes = length . getNotes

See http://haskell.org/hawiki/IterationPattern for some ideas.
 
Cheers,
Andrew Bromage


More information about the Haskell-Cafe mailing list