-- | A delimiter is a list of predicates to be matched to a subsequence. newtype Delimiter a = Delimiter [a -> Bool] -- | Try to match a delimiter at the start of a list, either failing -- or decomposing the list into the portion which matched the delimiter -- and the remainder. matchDelim :: Delimiter a -> [a] -> Maybe ([a],[a]) matchDelim (Delimiter []) xs = Just ([],xs) matchDelim (Delimiter _) [] = Nothing matchDelim (Delimiter (d:ds)) (x:xs) | d x = fmap (\(h,t) -> (x:h,t)) (matchDelim (Delimiter ds) xs) | otherwise = Nothing -- ... breakDelim :: Delimiter a -> [a] -> ([a],Maybe ([a],[a])) breakDelim d xxs = case matchDelim d xxs of Just match -> ([],Just match) Nothing -> case xxs of [] -> ([],Nothing) (x:xs) -> let (ys,match) = breakDelim d xs in (x:ys,match) -- ... onSublist :: Eq a => [a] -> Splitter a onSublist lst = defaultSplitter { delimiter = Delimiter (map (==) lst) } whenElt :: (a -> Bool) -> Splitter a whenElt p = defaultSplitter { delimiter = Delimiter [p] }