I appreciate that. I am actually familiar with Haskell and the Maybe monad.
Are there use cases for Maybe where you're not interacting with some sort of IO?
Re:Why isn't Haskell popular in industry?
by naradaellis on Monday August 02, 2010 @06:11AM
(#1567297)
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.
Ah duh. That makes sense.
Would it also make sense to use Maybe monad for converting a [Char] to an Integer?
stringToInt :: [Char] -> Maybe Integer
Re:Why isn't Haskell popular in industry?
by naradaellis on Monday August 02, 2010 @07:26AM
(#1567388)
Yeah it would. I think there was a Real World Haskell exercise that involved that actually.
Say you're working with the Maybe type, which is often used to represent operations that might fail, such as a map lookup, for example.
We're using two functions defined like so (feeling unimaginative at the moment, forgive me): We want to use them together. Someone who's not familiar with monads might write something like this: (If x works and returns a value, put that value into the y function. If x didn't work, just return Nothing)It works fine, but imagine you had 3 functions that returned Maybes, it would get tiresome and messy nesting all those case statements endlessly.
Not being satisfied with boilerplate, lets make an operation that will simplify this a bit: now we can define myFunction like so: if we want to add another operation: Congratulations you've mostly made a monad. Bind is one of the monad operators (>>=), the other operators are extremely trivial to implement for the Maybe type.This is why we say a monad is just a container with some handy operators. Maybe is the container, and bind is a way to chain together operations (without explicitly taking the value out of the container) so the operators themselves don't have to know anything about the nature of what they're dealing with.
I won't bang on anymore, this is a longer, better written tutorial[1] in the same vein as this short summary.
[1]: http://blog.sigfpe.com/2006/08/you-could-have-invented-monad...