3 ms·
Oh yeah sure all the time, Maybe shows up a lot. It becomes more useful and less of a pain once you realise you can deal with it using monad operators. From the
by naradaellis 16y ago
Oh yeah sure all the time, Maybe shows up a lot. It becomes more useful and less of a pain once you realise you can deal with it using monad operators. From the standard library a list operator and a map operator:
elemIndex :: a -> [a] -> Maybe Int
lookup :: k -> Map k a -> Maybe a
Say we have a map of lists of string, for some reason. We need to find the index of the element "foobar" in a particular key, so you could write:
lookup key map >>= elemIndex "foobar"
(a simple example for the sake of brevity)
Not very impressive, but monads aren't anything groundbreaking after all, they're just utilities that make our lives easier. There are more complicated operations that come in handy later, but they are quite straightforward once you get a grasp of the container metaphor.
- tkahn6 16y agoAh duh. That makes sense. Would it also make sense to use Maybe monad for converting a [Char] to an Integer? stringToInt :: [Char] -> Maybe Integer
- naradaellis 16y agoYeah it would. I think there was a Real World Haskell exercise that involved that actually.