In fact, you can write such a function by slightly changing the Set.insert function. I decided to return Maybe (Set a) , so the function creates a new Set if the element does not exist. It would also be possible to write a function with (Bool, Set a) as the return type.
insertMember :: Ord a => a -> Set a -> Maybe (Set a) insertMember = go where go :: Ord a => a -> Set a -> Maybe (Set a) STRICT_1_OF_2(go) go x Tip = Just $ singleton x go x (Bin sz ylr) = case compare xy of LT -> do l' <- go xl return $ balanceL yl' r GT -> do r' <- go xr return $ balanceR yl EQ -> Nothing
source share