Skip to content

Instantly share code, notes, and snippets.

@djwonk
Created February 6, 2009 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djwonk/59199 to your computer and use it in GitHub Desktop.
Save djwonk/59199 to your computer and use it in GitHub Desktop.
-- splitWith even [0,1,3,5,0,0,7,7,0,0] >>> [[1,3,5], [7,7]]
splitWith :: (a -> Bool) -> [a] -> [[a]]
splitWith f list = splitWithIter f ansl [] list
splitWithIter :: (a -> Bool) -> ([[a]] -> a -> [[a]]) -> [[a]] -> [a] -> [[a]]
splitWithIter _ _ xs [] = xs
splitWithIter f o xs (l:ls)
| f l = splitWithIter f ansl xs ls
| otherwise = splitWithIter f atlsl (o xs l) ls
-- Append New Sub List (ansl)
-- For example:
-- ansl [[1,2]] 3 >>> [[1,2], 3]
ansl :: [[a]] -> a -> [[a]]
ansl xs y = xs ++ [[y]]
-- Append to Last Sub List (atlsl)
-- For example:
-- atlsl [[1,2]] 3 >>> [[1,2,3]]
atlsl :: [[a]] -> a -> [[a]]
atlsl xs y = init xs ++ [last xs ++ [y]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment