[Haskell-cafe] strange stack overflow with Data.Map

David Menendez zednenem at psualum.com
Tue Jan 3 23:16:08 EST 2006


Christian Maeder writes:

> Adrian Hey wrote:
> > You mean am I proposing that Maps should retain old association
> > values and discard the new ones? If so the answer is no, of course
> > not. 
> 
> Actually, for maps it makes (even more) sense to have an additional 
> "insert-only-if-not-member" function, and be it only for (defining) 
> "fromList". Maybe such a function should simply be called "add" to 
> encourage its use.
> 
> {- | insert pair only if key is not a member yet (left-biased) leave
> map unchanged otherwise. -}
> add :: Ord a => Map a b -> a -> b -> Map a b
> 
> Properties:
> 
> insert k v m = add (delete k m) k v
> 
> add m k v = if member k m then m else insert k v m

Or, add m k v = insertWith (\_ oldV -> oldV) k v m
-- 
David Menendez <zednenem at psualum.com> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem>      |        of thermodynamics!"


More information about the Libraries mailing list