Haskell - rotate a list of lists into one list

If I have a list of lists, say [[1,2,3],[1,2,3],[1,2,3]] , is there a way in Haskell to turn this into just one list, for example [1,2,3,1,2,3,1,2,3] ?

Thanks in advance!

+4
source share
3 answers

Concat does what you want:

 concat [[1,2,3],[1,2,3],[1,2,3]] 

To find these features in the future, you can use hoogle http://www.haskell.org/hoogle/

You can search for a type — your required function is [[Int]] -> [Int] , so you can do this search . The top function is concat.

I have to mention that actually

 concat :: [[a]] -> [a] 

Thus, it works with any list of lists, and you can also quite happily find hoogle with this type. Hoogle is smart enough to figure out which types are appropriately close to what you requested, though.

+20
source

There are several ways to do this; you can use lists, for example:

 [y | x <- [[1,2,3],[1,2,3],[1,2,3]], y <- x] 

or to connect a function, then, in fact, in the same way:

 import Control.Monad (join) join [[1,2,3],[1,2,3],[1,2,3]] 

or concat function:

 concat [[1,2,3],[1,2,3],[1,2,3]] 

or msum (same with concat):

 import Control.Monad (msum) msum [[1,2,3],[1,2,3],[1,2,3]] 

or mconcat (same with concat):

 import Data.Monoid (mconcat) mconcat [[1,2,3],[1,2,3],[1,2,3]] 
+7
source

Direct answer

 > concat [[1,2,3],[1,2,3],[1,2,3]] [1,2,3,1,2,3,1,2,3] 

You should try hoogle whenever you want to search for any function. Sometimes the type is enough to get information about the function that you need.

+5
source

Source: https://habr.com/ru/post/1438492/


All Articles