user192837465
user192837465

Reputation: 215

Break string into substring in haskell

I am having a problem with my function, I want to do a pattern matching on a string with my function but I have a problem with breaking the string into substrings. I want for a string like "ccaabbccaacc" and a regular expresion like "a*b*c*" to obtain ["cc", "aabbcc", "aacc", ""], a list with the breaked subtring. I have made this function which returns all the parts of the substring

parts :: [a] -> [[[a]]]
parts [ ] = [[ ]]
parts [c] = [[[c]]]
parts (c : cs) = concat [[(c : p) : ps ,[c] : p : ps] | p : ps <- parts cs]

but when I apply my matchs function overt all the results it returns more that i want and i don't know how to filter the results. Can somene help me?

Upvotes: 0

Views: 662

Answers (1)

Will Ness
Will Ness

Reputation: 71119

I take it that this question is about parsing - that you want to break a string up into maximal chunks matching a given regexp, e.g. "a*b*c*".

That's like iterated lexer application, only with regexp. Assuming the existence of function

reglex :: String -> String -> (String, String)

that takes a regexp string, an input string, and returns a pair of longest matching prefix, and the rest of the input string, we can write

import Control.Arrow

parts reg str = ($ ("",str)) $
   iterate (reglex reg . snd) >>>
   tail >>>
   span (not.null.fst) 

and then do something with the result of this.

Upvotes: 1

Related Questions