Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/f.hs Secret

Created December 21, 2021 09:27
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 dminuoso/dc8cccaff1e72592b4f285153fbe4605 to your computer and use it in GitHub Desktop.
Save dminuoso/dc8cccaff1e72592b4f285153fbe4605 to your computer and use it in GitHub Desktop.
longestSuffix :: Domain -> DomTrie -> Maybe Domain
longestSuffix (Domain labels) table =
(Domain . reverse) <$> go (reverse labels) table Nothing
where
go [] _ r = r
go (x:xs) (DomTrie b table) (Just ts)
= case M.lookup x table of
Just sub | b -> go xs sub (Just (x:ts))
| otherwise -> go xs sub (Just ts)
Nothing -> Just ts
go (x:xs) (DomTrie b table) Nothing
= case M.lookup x table of
Just sub | b -> go xs sub (Just [x])
| otherwise -> go xs sub Nothing
Nothing -> Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment