Multisplit: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: (Replaced one If with a `case ... of`))
Line 676:
Or as a fold:
 
<lang haskell>importmultiSplit Data.Maybe:: [String] -> String -> [(isNothingString, String, fromJustInt)]
import Data.List (find, isPrefixOf)
 
multiSplit :: [String] -> String -> [(String, String, Int)]
multiSplit ds s =
let lng = length s
(tokensts, partsps, offseto) =
foldl
(\(tokens, parts, offset) (c, i) ->
let inDelim = offset > i
in case (if mb =inDelim
if inDelim then Nothing
then Nothing else find (`isPrefixOf` drop i s) ds) of
Just x -> else([], findmappend parts [(`isPrefixOf`tokens, dropx, i s)], dsi + length x)
in if isNothing mb Nothing ->
then ( mappend
tokens
(if inDelim
then []
else [c])
, parts
, offset))
else ( []
, mappend parts [(tokens, fromJust mb, i)]
, i + length (fromJust mb)))
([], [], 0)
(zip s [0 .. lng])
in mappend partsps [(tokensts, [], lng)]
 
main :: IO ()
main = print $ multiSplit ["==", "!=", "="] "a!===b=!=c"</lang>
</lang>
{{Out}}
<pre>[("a","!=",1),("","==",3),("b","=",6),("","!=",7),("c","",10)]</pre>