Created
May 6, 2013 07:10
-
-
Save Shekeen/5523749 to your computer and use it in GitHub Desktop.
This file contains 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
divideIntoParts :: [String] -> String -> [String] | |
divideIntoParts = divideIntoParts' [] "" | |
where | |
divideIntoParts' :: [String] -> String -> [String] -> String -> [String] | |
divideIntoParts' accList accStr _ "" = reverse ((reverse accStr) : accList) | |
divideIntoParts' accList accStr patterns str@(l:ls) = let (matched, rest) = matchPatterns patterns str | |
in | |
if null matched | |
then divideIntoParts' accList (l:accStr) patterns ls | |
else divideIntoParts' (matched : (reverse accStr) : accList) "" patterns rest | |
matchPatterns :: [String] -> String -> (String, String) | |
matchPatterns _ "" = ("", "") | |
matchPatterns patterns str = splitAt (matchedPatternLength patterns str) str | |
matchedPatternLength :: [String] -> String -> Int | |
matchedPatternLength patterns str = foldl (\ len pattern -> let (begin, end) = splitAt (length pattern) str in if and [len == 0, begin == pattern] then length pattern else len) 0 patterns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment