<div dir="ltr">I'm starting to get the hang of certain aspects of typeclasses, particularly with Maybe and list types. For instance I needed to write a function as follows:<div><br></div><div>Ord k => k -> Map k [a] -> Maybe a</div><div><br></div><div>which evaluates to "Nothing" if there is no such key in the map, or Just the first element of [a] if there is such a key, or Nothing if there is such a key but [a] is null.</div><div><br></div><div>So I could write</div><div><br></div><div>import qualified Data.Map as M</div><div>import Control.Monad</div><div>import Data.Maybe</div><div><br></div><div>f k m = case M.lookup k m of</div><div>  Nothing -> Nothing</div><div>  Just xs -> listToMaybe xs</div><div><br></div><div>But the case "Nothing -> Nothing" is suspicious... seems like that's always a clue some typeclass could simplify it. Eventually I figured out</div><div><br></div><div>f k = join . fmap listToMaybe . M.lookup k</div><div><br></div></div>