Splitting list into n-tuples [duplicate]
How can I split a list into list of tuples/lists of specified length? splitBy :: Int -> [a] -> [[a]]
splitBy 2 "asdfgh" should return ["as", "df", "gh"]
splitEvery
usually gets the nod for this job.
Searching Hoogle for Int -> [a] -> [[a]]
yields chunksOf
, which may be of use.
One way of doing it:
splitBy :: Int -> [a] -> [[a]]
splitBy _ [] = []
splitBy n xs = take n xs : splitBy n (drop n xs)
Another way of doing it:
splitBy' :: Int -> [a] -> [[a]]
splitBy' _ [] = []
splitBy' n xs = fst split : splitBy' n (snd split)
where split = splitAt n xs
精彩评论