
23 Jun
2010
23 Jun
'10
9:19 a.m.
A right-fold with a three-part accumulator is arguably simple and clear: sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound]) sortOutMusicData = foldr step ([],[],[]) where 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.