[Haskell-cafe] Is there a generic way to detect "mzero"?
chrisyco+haskell-cafe at gmail.com
Tue Mar 27 06:41:34 CEST 2012
On Tue, Mar 27, 2012 at 11:03 AM, Antoine Latter <aslatter at gmail.com> wrote:
> On Mon, Mar 26, 2012 at 4:25 PM, Ting Lei <tinlyx at hotmail.com> wrote:
>> Hi Antoine and Tobias (and everyone else),
>> Thanks a lot for your answers. They are really helpful
>> Can you please show me how to use the (Eq m) constraint to do this?
>> Also, my general question (probably novice-level) is that in monadic
>> programming, you can convert not necessarily monadic codes into monadic
>> I know for many cases, it is impossible to do the reverse conversion, e.g.
>> you can't make a function involving real IO operations into a pure code.
>> In other cases, for example, I may need to using things like Nothing as the
>> "null" value as in other programming languages, just to represent a special
>> "missing" value outside the regular type.
>> Is mzero a reasonable replacement for this or is there any reasonable
>> (abstract) approximation in Haskell for doing this? (Like "null", I need the
>> ability to detect it.)
> I think using 'Maybe' (with Nothing) is perfect for this - this
> function should come in handy:
It's often not good style to check Nothing explicitly, rather, it's
better to use monads to thread it through automatically.
If you have many functions that return a Maybe, then you can chain
them together using do syntax:
frobnicate = do
foo <- function1
bar <- function2 foo
return (bar + 1)
If any of the functions in the chain return Nothing, then the monad
will short circuit and the whole expression will result in Nothing.
The <- acts like an automatic null check.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe