[Haskell-beginners] a problem

Stephen Tetley stephen.tetley at gmail.com
Wed Jun 23 05:19:05 EDT 2010

A right-fold with a three-part accumulator is arguably simple and clear:

sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound])
sortOutMusicData = foldr step ([],[],[])
    step (Music_Data_1 n) (ns,ds,ss) = (n:ns, ds,   ss  )
    step (Music_Data_4 d) (ns,ds,ss) = (ns,   d:ds, ss  )
    step (Music_Data_9 s) (ns,ds,ss) = (ns,   ds,   s:ss)

Each step is simply a cons (:) to one of the tree lists which is
efficient. As the right fold takes you "backwards" through the list,
the orders of [Note], [Direction] etc. will be congruent with the
order of original input.

More information about the Beginners mailing list