Skip to content

Instantly share code, notes, and snippets.

@kevinadi
Created April 24, 2015 02:01
Show Gist options
  • Save kevinadi/b32da7b1054ca93b6d89 to your computer and use it in GitHub Desktop.
Save kevinadi/b32da7b1054ca93b6d89 to your computer and use it in GitHub Desktop.
Haskell: split a string to chunks
chunks :: Int -> [a] -> [[a]]
chunks _ [] = []
chunks n xs = (fst c) : chunks n (snd c)
where c = splitAt n xs
chunks' :: Int -> [a] -> [[a]]
chunks' n = unfoldr (\b -> if null b then Nothing else Just (splitAt n b))
chunks'' :: Int -> [a] -> [[a]]
chunks'' n = takeWhile (not.null) . unfoldr (Just . splitAt n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment