Skip to content

Instantly share code, notes, and snippets.

@kevinadi
Last active August 29, 2015 14:19
Show Gist options
  • Save kevinadi/da8fbd30b3e03300ce56 to your computer and use it in GitHub Desktop.
Save kevinadi/da8fbd30b3e03300ce56 to your computer and use it in GitHub Desktop.
Haskell: split a string using a substring
splitStr :: Eq a => [a] -> [a] -> [[a]]
splitStr sub str = split' sub str [] []
where
split' _ [] subacc acc = reverse (reverse subacc:acc)
split' sub str subacc acc
| sub `isPrefixOf` str = split' sub (drop (length sub) str) [] (reverse subacc:acc)
| otherwise = split' sub (tail str) (head str:subacc) acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment