[Haskell-beginners] error monad and Map lookup
Daniel Fischer
daniel.is.fischer at googlemail.com
Thu Dec 22 21:06:09 CET 2011
On Thursday 22 December 2011, 20:44:26, Dennis Raddle wrote:
> Is there a more elegant way to write 'test' in the following?
>
> import qualified Data.Map as M
> import Data.Map(Map)
> import Control.Monad.Error
>
> testMap :: Map Int String
> testMap = M.fromList [(1, "whatever"), (2, "dude")]
>
> test :: Int -> Either String String
> test x = do
> y <- case M.lookup x testMap of
> Nothing -> throwError "not in map"
> Just z -> return z
> return y
test = maybe (throwError "not in map") return . flip M.lookup testMap
More information about the Beginners
mailing list