[Haskell-beginners] error monad and Map lookup

Dennis Raddle dennis.raddle at gmail.com
Thu Dec 22 20:44:26 CET 2011


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111222/d1b43a93/attachment.htm>


More information about the Beginners mailing list