[Haskell-cafe] Implementing ParseChart with Data.Map

Krasimir Angelov kr.angelov at gmail.com
Mon Jun 2 18:33:24 EDT 2008


Not completely! This is a possible implementation:

case insertLookupWithKey (\_ -> Set.union) k (Set.singleton v) chart of
  (Nothing, chart) -> Just chart
  (Just set, chart) | Set.member v set -> Nothing
                          | otherwise             -> Just chart

but notice that the set is still traversed twice.

On Tue, Jun 3, 2008 at 12:07 AM, Neil Mitchell <ndmitchell at gmail.com> wrote:
> Hi Krasimir,
>
>> insert :: k -> v -> Chart k v -> Maybe (Chart k v)
>>
>> where the result is (Just _) if the (k,v) is actually added to the
>> chart or Nothing if it was already there and nothing have to be done.
>
> insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k
> a -> (Maybe a, Map k a)
>
> This should provide the information you need, without the double
> traversal of the data structure.
>
> Thanks
>
> Neil
>


More information about the Haskell-Cafe mailing list