[Haskell-beginners] figured out use for join!

Frerich Raabe raabe at froglogic.com
Wed Nov 11 22:01:29 UTC 2015

On 2015-11-11 22:12, Dennis Raddle wrote:
> f k m = case M.lookup k m of
>   Nothing -> Nothing
>   Just xs -> listToMaybe xs
> But the case "Nothing -> Nothing" is suspicious... seems like that's always 
> a clue some typeclass could simplify it. Eventually I figured out
> f k = join . fmap listToMaybe . M.lookup k

The 'Nothing -> Nothing' thing may also be a good hint that using the Maybe 
monad would be useful. Your function can also be defined as

   f k m = M.lookup k m >>= listToMaybe

Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing

More information about the Beginners mailing list