Skip to content

Instantly share code, notes, and snippets.

@rhz
Created July 7, 2011 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhz/1068694 to your computer and use it in GitHub Desktop.
Save rhz/1068694 to your computer and use it in GitHub Desktop.
import qualified Data.Map as Map
import qualified Data.Set as Set
type IntMap = Map.Map Int Int
type IntSet = Set.Set Int
extend :: IntMap -> IntSet -> IntMap -> Maybe (IntMap, IntSet)
extend inj codom map = Map.foldrWithKey f (Just (inj, codom)) map
where f :: Int -> Int -> Maybe (IntMap, IntSet) -> Maybe (IntMap, IntSet)
f i j (Just (inj, codom)) = if Set.member j codom
then Nothing
else Just ((Map.insert i j inj), (Set.insert j codom))
f _ _ _ = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment