Created
September 22, 2012 13:58
-
-
Save hanshoglund/3766241 to your computer and use it in GitHub Desktop.
Util to break up a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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