In the code it works well as below
divide :: Int -> Int -> Either String Int
divide a 0 = Left "Error with 0"
divide a b = Right (a `div` b)
The code to be changed
Map.adjust (divide 3) "A" $ Map.fromList [("A",3),("B",0)]
The expected result should be :
Map.adjust (divide 3) "A" $ Map.fromList [("A",3),("B",0)]
=> Right $ Map.fromList [("A",1),("B",0)]
Map.adjust (divide 3) "B" $ Map.fromList [("A",3),("B",0)]
=> Left "Error with 0"
Or in general how to build a function like:
Map.adjust:: (a -> m a) -> k -> (Map k a) -> m (Map k a)
Thank you very much !
Monad m
, or isEither
/Maybe
sufficient?adjust:: (a -> m b) -> k -> (Map k a) -> m (Map k b)
seems suspect, because it can change the type of the whole map even if the functions only modifies a single value inside it.a -> m a
.