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

≡放荡痞女 提交于 2019-11-28 13:41:10

I believe map return or map pure are good enough.

Maybe this?

map (\x -> [x]) xs

Yours can work on any functor I think so this would be more idomatic for just lists.

The split package provides a (Data.List.Split.)chunksOf function whose name is, IMO, more meaningful than the various map solutions (even if they are more idiomatic.)

You can also use a list comprehension:

[ [x] | x <- theList]

Maybe overkill for such a simple example, but depending on your context, maybe you can merge this step with some further processing of the singleton lists:

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

Tongue-in-cheek version:

import Data.List

groupBy (const . const False) xs

Using do notation:

do { x <- xs; return [x] }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!