June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
25
Task/Multisplit/Haskell/multisplit-2.hs
Normal file
25
Task/Multisplit/Haskell/multisplit-2.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import Data.List (find, isPrefixOf)
|
||||
|
||||
multiSplit :: [String] -> String -> [(String, String, Int)]
|
||||
multiSplit ds s =
|
||||
let lng = length s
|
||||
(ts, ps, o) =
|
||||
foldl
|
||||
(\(tokens, parts, offset) (c, i) ->
|
||||
let inDelim = offset > i
|
||||
in case (if inDelim
|
||||
then Nothing
|
||||
else find (`isPrefixOf` drop i s) ds) of
|
||||
Just x -> ([], (tokens, x, i) : parts, i + length x)
|
||||
Nothing ->
|
||||
( if inDelim
|
||||
then tokens
|
||||
else c : tokens
|
||||
, parts
|
||||
, offset))
|
||||
([], [], 0)
|
||||
(zip s [0 .. lng])
|
||||
in reverse $ (ts, [], lng) : ps
|
||||
|
||||
main :: IO ()
|
||||
main = print $ multiSplit ["==", "!=", "="] "a!===b=!=c"
|
||||
Loading…
Add table
Add a link
Reference in a new issue