[Haskell-cafe] MonadPlus and Alternative folds

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Sat Sep 19 17:38:47 UTC 2015


On Sat, Sep 19, 2015 at 06:13:18PM +0100, Tom Ellis wrote:
> On Sat, Sep 19, 2015 at 01:25:00AM -0400, David Feuer wrote:
> > It kind of seems like there should be fold-like things to match some and
> > many.
> > 
> > foldrAlt :: Alternative f => (a -> b -> b) -> b -> f a -> f b
> > foldrAlt c n m = foldr1Alt c n m <|> pure n
> > 
> > foldr1Alt :: Alternative f => (a -> b -> b) -> b -> f a -> f b
> > foldr1Alt c n m = c <$> m <*> foldrAlt c n m
> [...]
> 
> An Alternative allows list-like behaviour when it comes to *construction*,
> but offers no operation analogous to pattern-matching and therefore doesn't
> allow you to look inside it or do any sort of *destruction*.
> 
> I tried foldrAlt and it looped forever, as I expected.  I also expect the
> others to loop forever.

I now understand this can work for some Alternatives.  The operational reading
is

1. Try to read an a
2. If it fails, return the b
3. If it succeeds, update the b
4. Go to 1

However, since it doesn't work even for an Alternative as simple as Maybe
it's worth thinking about what class of Alternatives we really want it to
apply to.

Tom


More information about the Haskell-Cafe mailing list