Useful Haskell List Function
July 12, 2009 at 3:09 pm | In haskell | Leave a CommentI keep finding myself with a long list of elements, which I need to split into a list of lists of those elements. For example, I need to go from [0,1,2,3,4,5,6,7,8] to [[0,1,2],[3,4,5],[6,7,8]].
So far, the best function I have found to do this is
split :: Int -> [a] -> [[a]]
split n = unfoldr (\r -> case r of [] -> Nothing; x -> Just $ splitAt n x)
But that seems a little bit overcomplicated for such a simple function. Am I missing a better, more obvious solution?
No Comments Yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.