我一直在使用((:[]) <$> xs)
,但如果有更清晰的方法,我很乐意使用它。
编辑:这么多好答案的家伙!我不认为我可以接受一个,因为它们都很好。
我相信map return
或map pure
足够好。
也许这个?
map (\x -> [x]) xs
你的可以在我认为的任何函子上工作,所以这对于列表来说更符合规范。
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]
split 包提供了一个 (Data.List.Split.)chunksOf 函数,其名称为 IMO,比各种地图解决方案更有意义(即使它们更惯用。)
诙谐版本:
import Data.List
groupBy (const . const False) xs
使用do
符号:
do { x <- xs; return [x] }