I am working an assignment in Haskell, to prepare for tests. The current task asks to tokenize a string after the following formula: When running "tokenize str separate remove" it should should output a list of strings. Every character in "str" appearing in string "separate" should be a string of one character. Every character in "str" appearing in string "remove" should be removed. Characters not appearing in either separate or remove should be bundled together.
Example shows that
tokenize "a + b* 12-def" "+-*" " "
should output
["a", "+", "b", "*", "12", "-", "def"]
my current code below
tokenize :: String -> String -> String -> [String]
tokenize [] _ _ = []
tokenize [x] _ _ = [[x]]
tokenize (x:xs) a b | x `elem` a = [x] : tokenize xs a b
| x `elem` b = tokenize xs a b
| otherwise = (x:head rest) : tail rest
where
rest = tokenize xs a b
It works to some extent, the problem being that it the operators in the example is bundled with the letter preceding it.
like this
["a+","b*","12-","def"]
despite the operators being in the separate string.