I am attempting to write my own function which takes an arbitrary number of tokens, and then splits an arbitrary string on any of those.
After a little bit of thinking, I believe I need to recursively iterate through a list of tokens, and then pass each split list into a map with the splitting function, followed by a flatten.
At present, my algorithm looks like so:
module MyAwesomeModule where
import qualified Data.Text as T
outputSplit :: String -> [String] -> IO ()
outputSplit s tokens = print $ splitRecursive tokens s
splitRecursive :: [String] -> String -> [String]
splitRecursive tokens s = splitOneOf tokens s
splitOneOf :: [String] -> String -> [String]
splitOneOf [] s = []
splitOneOf (t:tokens) s = map (splitOneOf tokens)(map (T.unpack) (T.splitOn (T.pack t) (T.pack s))) ++ (splitOneOf tokens s)
Which errors out with:
Couldn't match type `[Char]' with `Char'
Expected type: String -> String
Actual type: String -> [String]
In the return type of a call of `splitOneOf'
In the first argument of `map', namely `(splitOneOf tokens)'
In the first argument of `(++)', namely
`map
(splitOneOf tokens)
(map (T.unpack) (T.splitOn (T.pack t) (T.pack s)))'
So as far as I understand, what that means is that the Strings in the initial split are being cast to [Char]
Prelude > let a = (map (T.unpack) (T.splitOn (T.pack "a") (T.pack "abcdefabc")))
["","bcdef","bc"]
:t a
a::[String]
let b = head a
:t b
b::String
Moreover, if splitOneOf is defined as:
splitOneOf :: [String] -> String -> [String]
splitOneOf [] s = []
splitOneOf (t:tokens) s = (map (T.unpack) (T.splitOn (T.pack t) (T.pack s))) ++ (splitOneOf tokens s)
then
Prelude > let a = splitOneOf ["a", "b"] "abcdefghij"
["", "bcdefghij"]
map (splitOneOf ["b"]) a
[[""], [[""],["cdefghij"]]
What exactly is going on with the type signatures here? Is this the right way to map? What am I missing?
splitOneOf tokensin parantheses?concatMapandsplitOnto get something of typeString -> [String] -> [String](first argument is a token). Then use a common recursion encapsulating function to repeatedlt apply this to a list if tokens.