[Haskell-cafe] guards in applicative style

Lorenzo Bolla lbolla at gmail.com
Wed Sep 12 16:52:03 CEST 2012


I'm no expert at all, but I would say "no".
"guard" type is:
guard :: MonadPlus m => Bool -> m ()

and "MonadPlus" is a monad "plus" (ehm...) mzero and mplus
(http://en.wikibooks.org/wiki/Haskell/MonadPlus).
On the other hand Applicative is "less" than a monad
(http://www.haskell.org/haskellwiki/Applicative_functor), therefore
"guard" as is cannot be defined.

But, in your specific example, with lists, you can always use "filter":
filter (uncurry somePredicate) ((,) <$> list1 <*> list2 (somePredicate ???))

hth,
L.


On Wed, Sep 12, 2012 at 3:40 PM, felipe zapata <tifonzafel at gmail.com> wrote:
>
> Hi Haskellers,
>
> Suppose I have two list and I want to calculate
> the cartesian product between the two of them,
> constrained to a predicate.
> In List comprehension notation is just
>
> result = [ (x, y) | x <- list1, y <-list2, somePredicate x y ]
>
> or in monadic notation
>
> result = do
>  x <- list1
>  y <- list2
>  guard (somePredicate x y)
> return $ (x,y)
>
> Then I was wondering if we can do something similar using an applicative style
>
> result = (,) <$> list1 <*> list2 (somePredicate ???)
>
> The question is then,
> there is a way for defining a guard in applicative Style?
>
> Thanks in advance,
>
> Felipe Zapata.
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list