[Haskell-cafe] IO (Either a Error) question
Ivan Lazar Miljenovic
ivan.miljenovic at gmail.com
Sat May 8 00:15:41 EDT 2010
David Menendez <dave at zednenem.com> writes:
>
> I wonder how often people rely on the use of fail in pattern matching.
> Could we get by without fail or unfailable patterns?
>
> ensureCons :: MonadPlus m => [a] -> m [a]
> ensureCons x@(_:_) = return x
> ensureCons _ = mzero
>
> do ...
> x:xs <- ensureCons $ some_compuation
>
> This is more flexible than the current situation (you can easily adapt
> it to throw custom exceptions in ErrorT), but gets cumbersome when
> you're doing nested patterns. Also, it does the match twice, but
> presumably the optimizer can be improved to catch that if the idiom
> became popular.
Well, any time you have a do-block like this you're using failable
patterns:
maybeAdd :: Maybe Int -> Maybe Int -> Maybe Int
maybeAdd mx my = do x <- mx
y <- my
return $ x + y
--
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com
More information about the Haskell-Cafe
mailing list