Last active
September 13, 2020 14:59
-
-
Save paolino/e709244485bbe81c92c3d94de0000873 to your computer and use it in GitHub Desktop.
extract an enclosed sublist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extract :: Eq a => [a] -> [a] -> [a] -> Maybe [a] | |
extract start end xs = do | |
ys <- snd <$> split start xs | |
fst <$> split end ys | |
split :: Eq a => [a] -> [a] -> Maybe ([a], [a]) | |
split p = go id | |
where | |
go _ [] = Nothing | |
go f xt@(x : xs) = | |
case match p xt of | |
Nothing -> go (f . (x :)) xs | |
Just rs -> Just (f [], rs) | |
match :: Eq a => [a] -> [a] -> Maybe [a] | |
match [] rs = Just rs | |
match _ [] = Nothing | |
match (x : xs) (y : ys) | |
| x == y = match xs ys | |
| otherwise = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment