Interleave two lists

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


2008/8/24 Sean Leather <leather at cs.uu.nl>:
> 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
Transformers"

<http://www.cs.rutgers.edu/~ccshan/logicprog/LogicT-icfp2005.pdf>

The logict package has an implementation

<http://hackage.haskell.org/cgi-bin/hackage-scripts/package/logict>

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 zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Libraries mailing list