Alistair Bayley | 16 May 12:12

Std lib equivalent for liftM concat . sequence

A couple of days ago I had need for:

> concatM :: Monad m => [m [a]] -> m [a]
> concatM = liftM concat . sequence

but found no such thing in the std libs, except perhaps for msum (I
don't want to add instances for MonadPlus. Should I have to?). Have I
missed something trivial?

Alistair
Bulat Ziganshin | 16 May 12:32
Picon

Re: Std lib equivalent for liftM concat . sequence

Hello Alistair,

Friday, May 16, 2008, 2:12:45 PM, you wrote:

>> concatM = liftM concat . sequence

> but found no such thing in the std libs, except perhaps for msum (I
> don't want to add instances for MonadPlus. Should I have to?). Have I
> missed something trivial?

1. it's easy to define yourself
2. it's not widely used. at least, i have in my program for, foreach,
concatMapM, filterM and lot of other monadic operations, but no one
concatM

--

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin <at> gmail.com
Yitzchak Gale | 18 May 15:58

Re: Std lib equivalent for liftM concat . sequence

Alistair Bayley wrote:
>> A couple of days ago I had need for:
>> concatM :: Monad m => [m [a]] -> m [a]
>> concatM = liftM concat . sequence
>> but found no such thing in the std libs

It used to be in an older version of Haskell, but
it was removed in Haskell 98.

Bulat Ziganshin wrote:
> 2. it's not widely used.

Bulat is justifiably concerned about backwards compatibility
issues when changing the core standard libraries, including
when adding new functions.

Despite Bulat's best efforts, there has been a trend lately
of adding those kinds of things.

If so, nowadays it can be made more general. In this
case, you could at least generalize it to:

concatM :: (Monad m, Traversable t, Foldable t, MonadPlus p) =>
  t (m (p a)) -> m (p a)

or

concatA :: (Applicative f, Traversable t, Foldable t, MonadPlus p) =>
  t (f (p a)) -> f (p a)

(Continue reading)

Miguel Mitrofanov | 16 May 18:13
Picon
Favicon

Re: Std lib equivalent for liftM concat . sequence

Seems to be close to

sequence :: [ListT m a] -> ListT m a

Hmm?

On 16 May 2008, at 14:12, Alistair Bayley wrote:

> A couple of days ago I had need for:
>
>> concatM :: Monad m => [m [a]] -> m [a]
>> concatM = liftM concat . sequence
>
> but found no such thing in the std libs, except perhaps for msum (I
> don't want to add instances for MonadPlus. Should I have to?). Have I
> missed something trivial?
>
> Alistair
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
Miguel Mitrofanov | 16 May 19:51
Picon
Favicon

Re: Std lib equivalent for liftM concat . sequence

Oops, I was very wrong. Sorry.

On 16 May 2008, at 20:13, Miguel Mitrofanov wrote:

> Seems to be close to
>
> sequence :: [ListT m a] -> ListT m a
>
> Hmm?
>
> On 16 May 2008, at 14:12, Alistair Bayley wrote:
>
>> A couple of days ago I had need for:
>>
>>> concatM :: Monad m => [m [a]] -> m [a]
>>> concatM = liftM concat . sequence
>>
>> but found no such thing in the std libs, except perhaps for msum (I
>> don't want to add instances for MonadPlus. Should I have to?). Have I
>> missed something trivial?
>>
>> Alistair
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe <at> haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
(Continue reading)

Yitzchak Gale | 18 May 16:05

Re: Std lib equivalent for liftM concat . sequence

Alistair Bayley wrote:
>> concatM :: Monad m => [m [a]] -> m [a]
>> concatM = liftM concat . sequence

Miguel Mitrofanov wrote:
> Seems to be close to
> sequence :: [ListT m a] -> ListT m a
> Hmm?

Yes. It is close to something like that for the old
broken ListT - but let's not talk about that one.

For "ListT done right", how about:

concatM :: Monad m => [ListT m a] -> List m a
concatM = join . liftList

(http://www.haskell.org/haskellwiki/ListT_done_right)

Regards,
Yitz

Gmane