[Haskell-beginners] some Data.Map questions

David McBride dmcbride at neondsl.com
Fri Aug 19 16:21:25 CEST 2011


Well, if you use that instruction type (you can call it whatever you
want), you should be able to use 'map' to

notes2instr :: Map Loc Note -> Map Loc Instr
dirs2instr :: Map Loc PlayDirection -> Map Loc Instr

which might simplify things a little from what I originally posted.  I
thought you might be able to use unionWithKey to combine, but that
would still be wrong because you cannot be sure what playdirection you
are in when you hit a specific note.

There is no function in Data.Map that will automatically tuple or
combine two different types of maps for you because there are so many
ways it could be done.  Even if it did, your requirements are specific
enough that you'll have to do the work yourself to get the right
answer.

On Fri, Aug 19, 2011 at 2:40 AM, Dennis Raddle <dennis.raddle at gmail.com> wrote:
> Hi David,
> This helps, for sure. I wondered if any of the rich selection of
> functions in Data.Map could be used, such as union. To perform a union
> on two maps, their elements must have the same type. From this one
> concludes that a type unifying Note's and PlayDirection's is
> necessary, which is what you have done in Instruction.
>
> On Thu, Aug 18, 2011 at 8:46 PM, David McBride <dmcbride at neondsl.com> wrote:
>> Maybe something like this:
>>
>> instance Eq Loc where
>>  a == b = ...
>> instance Ord Loc where
>>  compare a b = ...
>>
>> data Instruction = InstrNote (Loc, Note) | InstrPD (Loc, PlayDirection)
>>
>> write a function that will combine the notes and playdirections into
>> one list of everything sequentially.
>>
>> combineNotesDirs :: [(Loc,Note)] -> [(Loc,PlayDirection)] -> [Instruction]
>> combineNotesDirs [] [] = []
>> combineNotesDirs [] (d:ds) = undefined --you can figure this out
>> combineNotesDirs (n:ns) [] = undefined
>> combineNotesDirs notes@(n:ns) dirs@(d:ds)
>>  | fst d <= fst n = InstrPD d : (zipNotesDirs notes ds)
>>  | otherwise      = InstrNote n : (zipNotesDirs ns dirs)
>>
>> Then loop over that and store them as a map.  Traverse the list, keep
>> track of the playdirection at each entry, and then accumulate the
>> current direction and note at each note entry into a new map.
>>
>> instructionlist2map :: [Instruction] -> Map Loc (Note, PlayDirection)
>>
>> I'm sure this could be cleaned up a bit, but is that sort of what you
>> were looking for?
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list