Concise way to conditionally update map in State monad
问题 Below is the code from an answer regarding memoization, showing a memoization function used in the State monad, where the state is updated with the result of the passed function if the key is not already in the map. type MyMemo a b = State (Map.Map a b) b myMemo :: Ord a => (a -> MyMemo a b) -> a -> MyMemo a b myMemo f x = do map <- get case Map.lookup x map of Just y -> return y Nothing -> do y <- f x modify $ \map' -> Map.insert x y map' return y It doesn't seem like idiomatic Haskell: it