Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been using ((:[]) <$> xs) but if there is a more clear way I would love to use it.

edit: so many good answers guys! I don't think I can accept one because they are all good.

like image 987
Drew Avatar asked Jul 05 '13 18:07

Drew


3 Answers

I believe map return or map pure are good enough.

like image 174
is7s Avatar answered Sep 20 '22 13:09

is7s


Maybe this?

map (\x -> [x]) xs

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

like image 20
DiegoNolan Avatar answered Sep 23 '22 13:09

DiegoNolan


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.)

like image 44
Frédéric Avatar answered Sep 20 '22 13:09

Frédéric