Skip to content

Instantly share code, notes, and snippets.

@hanshoglund
Created September 22, 2012 13:58
Show Gist options
  • Save hanshoglund/3766241 to your computer and use it in GitHub Desktop.
Save hanshoglund/3766241 to your computer and use it in GitHub Desktop.
Util to break up a list
import qualified Data.List
import qualified Data.Monoid
-- | Divide a list into parts of maximum length n.
divideList :: Int -> [a] -> [[a]]
divideList n xs
| length xs <= n = [xs]
| otherwise = [take n xs] ++ (divideList n $ drop n xs)
-- | Break up a list into parts of maximum length n, inserting the given list as separator.
-- Usefor for breaking up strings, as in @breakList 80 "\n" str@.
breakList :: Int -> [a] -> [a] -> [a]
breakList n z = Data.Monoid.mconcat . Data.List.intersperse z . divideList n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment