
Can I get a suggestion for a concise way to write 'sortOutMusicData' as described here? This is MusicXML-related. data Music_Data_ = Music_Data_1 Note | Music_Data_4 Direction | Music_Data_9 Sound ... sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound])

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.

Hello,
I've got (not that short):
conv :: Music_Data_ -> (Maybe Note, Maybe Direction, Maybe Sound)
conv (Music_Data_1 n) = (Just n, Nothing, Nothing)
conv (Music_Data_4 d) = (Nothing, Just d, Nothing)
conv (Music_Data_9 s) = (Nothing, Nothing, Just s)
sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound])
sortOutMusicData d = catMaybes3 $ unzip3 $ map conv d
catMaybes3 (a,b,c) = (catMaybes a, catMaybes b, catMaybes c)
Cordialement,
Corentin DUPONT (SII)
CDL AL4/AT8 et Comelec
DTI/DPMO/CSEE/CLCO/LCTL
Tel: +33 (0) 1 56 47 65 38 / 47 65 38
corentin.dupont@ext.mpsa.com
Michael Mossey

Hello,
I would do (no that concise):
toMaybe :: Music_Data_ -> (Maybe Note, Maybe Direction, Maybe Sound)
toMaybe (Music_Data_1 n) = (Just n, Nothing, Nothing)
toMaybe (Music_Data_4 d) = (Nothing, Just d, Nothing)
toMaybe (Music_Data_9 s) = (Nothing, Nothing, Just s)
catMaybes3 (a,b,c) = (catMaybes a, catMaybes b, catMaybes c)
sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound])
sortOutMusicData = catMaybes3 . unzip3 . map toMaybe
Corentin
Michael Mossey

Hello,
I would do (not that concise):
toMaybe :: Music_Data_ -> (Maybe Note, Maybe Direction, Maybe Sound)
toMaybe (Music_Data_1 n) = (Just n, Nothing, Nothing)
toMaybe (Music_Data_4 d) = (Nothing, Just d, Nothing)
toMaybe (Music_Data_9 s) = (Nothing, Nothing, Just s)
catMaybes3 (a,b,c) = (catMaybes a, catMaybes b, catMaybes c)
sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound])
sortOutMusicData = catMaybes3 . unzip3 . map toMaybe
Corentin
Michael Mossey

On 25 June 2010 14:33,
Hello, I would do (not that concise):
toMaybe :: Music_Data_ -> (Maybe Note, Maybe Direction, Maybe Sound) toMaybe (Music_Data_1 n) = (Just n, Nothing, Nothing) toMaybe (Music_Data_4 d) = (Nothing, Just d, Nothing) toMaybe (Music_Data_9 s) = (Nothing, Nothing, Just s)
catMaybes3 (a,b,c) = (catMaybes a, catMaybes b, catMaybes c)
sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound]) sortOutMusicData = catMaybes3 . unzip3 . map toMaybe
Corentin
Hi Corentin Why would you do that though? It's doing quite a bit more work - introducing the extra constructors through the Nothing/Just's and extra traversals through the three catMaybe's in the post-processing step. And its longer too...

Hello Stephen,
I've just look at your solution, it is better ;)
Stephen Tetley
Hello, I would do (not that concise):
toMaybe :: Music_Data_ -> (Maybe Note, Maybe Direction, Maybe Sound) toMaybe (Music_Data_1 n) = (Just n, Nothing, Nothing) toMaybe (Music_Data_4 d) = (Nothing, Just d, Nothing) toMaybe (Music_Data_9 s) = (Nothing, Nothing, Just s)
catMaybes3 (a,b,c) = (catMaybes a, catMaybes b, catMaybes c)
sortOutMusicData :: [Music_Data_] -> ([Note],[Direction],[Sound]) sortOutMusicData = catMaybes3 . unzip3 . map toMaybe
Corentin
Hi Corentin Why would you do that though? It's doing quite a bit more work - introducing the extra constructors through the Nothing/Just's and extra traversals through the three catMaybe's in the post-processing step. And its longer too... _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi Corentin Thanks - I wasn't meaning to bash your solution outright, but sometimes a points free or "chain-of-compose" style solution is less efficient and more long winded than using direct recursion or a fold. I'd contend this is one such case.
participants (3)
-
corentin.dupont@ext.mpsa.com
-
Michael Mossey
-
Stephen Tetley