I'm very new to Haskell. I'd like to be able to find some color expressions in a string. So let's say I have this list of expressions:
colorWords = ["blue", "green", "blue green"]
And I want to be able to get the locations of all of those, anywhere in a string, even if it's broken up by a linebreak, or if a hyphen separates it instead. So given a string like:
First there was blue
and then there was Green,
and then blue
green all of a sudden, and not to mention blue-green
It should give the character offsets for "blue" (line one), "green" (line two), and "blue green" (lines 3-4) and "blue-green" (line 4), something like:
[("blue", [20]), ("green", [40]), ("blue green", [50, 65])]
I can do this with regexes, but I've been trying to do it with a parser just as an exercise. I'm guessing it's something like:
import Text.ParserCombinators.Parsec
separator = spaces <|> "-" <|> "\n"
colorExp colorString = if (length (words colorString))>1 then
multiWordColorExp colorString
else colorString
multiWordColorExp :: Parser -> String
multiWordColorExp colorString = do
intercalate separator (words colorString)
But I have no idea what I'm doing, and I'm not really getting anywhere with this.
sepBy
or similar to iterate it.