JasperTack
JasperTack

Reputation: 4457

return maybe value when referencing key-value list

I use key-value lists in haskell and I would like to get the value Nothing when the key is not found in the list instead of the exception. For example:

*> x
fromList [(1,[(6,14.0),(3,9.0),(2,7.0)]),(2,[(4,15.0),(3,10.0)]),(3,[(6,2.0),(4,11.0)]),(5,[(4,6.0)]),(6,[(5,9.0)])]
*> x ! 7
*** Exception: Map.find: element not in the map

is it possible to do this?

Upvotes: 1

Views: 94

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120741

LMHTFY: Map k a -> k -> Maybe a.

The very first result is Map.lookup, which is what you want.

Upvotes: 3

Related Questions