Haskell: Is there an idiomatic way to insert each item into a list in its own list?

I use ((:[]) <$> xs) , but if there is a clearer way, I would love to use it.

Edit: so many good answers guys! I donโ€™t think I can accept it because they are all good.

+6
source share
6 answers

I believe that map return or map pure are good enough.

+12
source

Maybe this?

 map (\x -> [x]) xs 

Yours can work on any functor, I think it would be more ideal for lists only.

+7
source

The split package provides the function (Data.List.Split.) ChunksOf, whose name is IMO, is more significant than various card solutions (even if they are more idiomatic).

+4
source

You can also use list comprehension:

 [ [x] | x <- theList] 

Maybe a redundancy for such a simple example, but depending on your context, perhaps you can combine this step with further processing of singleton lists:

 [f [x] + 13 | x <- theList] 
+4
source

Tongue in the buccal version:

 import Data.List groupBy (const . const False) xs 
+2
source

Using the do notation:

 do { x <- xs; return [x] } 
+1
source

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


All Articles