tags:

views:

101

answers:

1

I must compile on a recent version of Haskell a software written for a previous version of the standard libraries. The code assumes Data.Map.lookup has type:

lookup :: (Monad m, Ord k) => k -> Map k a -> m a

as it was the case with e.g. GHC 6.8.1/containers 0.1.0.0., but since (at least) GHC 6.10.1/containers 0.2.0.0 Data.Map.lookup has type:

lookup :: Ord k => k -> Map k a -> Maybe a

Since I don't know much of Haskell I'm looking for a workaround or an equivalent function in the current libraries. Can anyone help me?

+3  A: 

Just find out what old lookup did in each possible case and replicate the functionality.

What did occur in case the element did not exist in the map? I guess, fail is called?

Then it would be

lookup2 el map =
  case lookup el map of
    Just x -> return x
    Nothing -> fail "Element doesn't exist in the map"
Dario
cleaned up: `lookupM el = maybe (fail "Element not in map") return . lookup el`
sclv
@sclv: True, tacit style is always nice ;) But since the OP said he doesn't know much of Haskell, I wanted to be explicit (and thus somewhat more verbose)
Dario
Thank you, it works!
Pietro