[Haskell-cafe] guards in applicative style

Ryan Ingram ryani.spam at gmail.com
Mon Sep 17 19:47:09 CEST 2012


Not exactly what you asked for, but...

    filter (uncurry somePredicate) $ (,) <$> list1 <*> list2

does the job.

Using only applicative operations, it's impossible to affect the 'shape' of
the result--this is the difference in power between applicative and monad.

  -- ryan


On Wed, Sep 12, 2012 at 7:40 AM, 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120917/99b0a5c8/attachment.htm>


More information about the Haskell-Cafe mailing list