[Haskell-beginners] Alternative

Yitzchak Gale gale at sefer.org
Wed Dec 28 23:24:25 UTC 2016

Imants Cekusins wrote:
> Yitzchak, how about this:
> Prelude Control.Applicative> foldl (<|>) Nothing [Just 1, Nothing, Just 2,
> Nothing]
> Just 1
> Prelude Control.Applicative> foldr (<|>) Nothing [Just 1, Nothing, Just 2,
> Nothing]
> Just 1
> ? I guess this is due to the nature of (<|>), no?

Yes, you are right. <|> for Maybe satisfies the associative law:

(x <|> y) <|> z == x <|> (y <|> z)

for all x, y, and z. So it does not matter if you apply it from
right to left or from left to right.

> Anyway, I'd use asum instead of fold_ for Alternatives.

Agreed. For the actual problem, Ollie is right.

I just thought you might be interested in these other concepts
that come up from your good thinking.


More information about the Beginners mailing list