Interleave two lists

David Menendez dave at
Sun Aug 24 20:40:03 EDT 2008

2008/8/24 Sean Leather <leather at>:
> Does (|||) seem useful to others? Is it already available in some other form
> (or in a library) of which I'm not aware? If yes and no are the answers,
> then I wonder if it's useful enough for Data.List (modulo any expected
> renaming).

There's a generalized version of (|||) called "interleave" defined in
the paper "Backtracking, Interleaving, and Terminating Monad


The logict package has an implementation


In short, logict provides a class MonadLogic whose members support
interleave, among other functions:

    class (MonadPlus m) => MonadLogic m where
        msplit :: m a -> m (Maybe (a, m a))
        interleave :: m a -> m a -> m a
        interleave a b = msplit a >>= maybe b (\(x,a') -> return x
`mplus` interleave b a')

    instance MonadLogic [] where
        msplit [] = [Nothing]
        msplit (x:xs) = [Just (x,xs)]

The main disadvantage interleave and (|||) have, compared to mplus and
(++), is that they aren't associative.

Dave Menendez <dave at>

More information about the Libraries mailing list