Proposal: add new function "check" to Control.Monad

David Menendez dave at zednenem.com
Wed Aug 26 15:03:44 EDT 2009


On Wed, Aug 26, 2009 at 2:07 PM, Jon
Fairbairn<jon.fairbairn at cl.cam.ac.uk> wrote:
> On 2009-08-26 at 09:49PDT Iavor Diatchki wrote:
>> Hello,
>> I don't really think that we need this function, it is plenty easy to
>> just write with "guard".
>
> See my response to Sebastian, but also, none of the definitions
> of check in terms of guard meet my criteria for being simple
> enough, namely no lambdas and only a small amount of plumbing
> (ie don't get round the "no lambdas" part by compiling it to S
> and K ;-)

I'm usually opposed to adding small functions to the standard library,
but check is something I've defined for myself dozens of times.
(Although I usually call it "require".)

In my experience, check is very natural if you're writing code using
>>= (and especially =<<), but guard is possibly more natural with
do-notation. Similarly, guard scales better if you have multiple
conditions. e.g.,

    foo >>= require even >>= bar

is more convenient than

    foo >>= \x -> guard (even x) >> bar x

but

    foo >>= require (\x -> even x && x < y) >>= bar

is no better than

    foo >>= \x -> guard (even x && x < y) >> bar x


On the other hand, the more applicative style,

    bar =<< require even =<< foo

doesn't easily translate into using guard.


Finally, it's worth considering adding filter (or "mfilter", "sieve",
whatever). It's trivial to define check/require in terms of filter,
and the resulting code looks even cleaner.

    filter even foo >>= bar
    filter (\x -> even x && x < y) foo >>= bar
    bar =<< filter even foo

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Libraries mailing list