6

我一直在使用((:[]) <$> xs),但如果有更清晰的方法,我很乐意使用它。

编辑:这么多好答案的家伙!我不认为我可以接受一个,因为它们都很好。

4

6 回答 6

12

我相信map returnmap pure足够好。

于 2013-07-05T19:03:10.967 回答
7

也许这个?

map (\x -> [x]) xs

你的可以在我认为的任何函子上工作,所以这对于列表来说更符合规范。

于 2013-07-05T18:54:21.290 回答
4

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]
于 2013-07-05T22:43:33.863 回答
4

split 包提供了一个 (Data.List.Split.)chunksOf 函数,其名称为 IMO,比各种地图解决方案更有意义(即使它们更惯用。)

于 2013-07-05T20:56:03.307 回答
2

诙谐版本:

import Data.List

groupBy (const . const False) xs
于 2013-07-06T11:05:50.720 回答
1

使用do符号:

do { x <- xs; return [x] }
于 2013-07-06T06:20:34.110 回答