Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@paolino
Last active September 13, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paolino/e709244485bbe81c92c3d94de0000873 to your computer and use it in GitHub Desktop.
Save paolino/e709244485bbe81c92c3d94de0000873 to your computer and use it in GitHub Desktop.
extract an enclosed sublist
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