-
-
Save dminuoso/41d18409f95b98963df7bdff681ecdbd to your computer and use it in GitHub Desktop.
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
longestSuffix :: Odin.Domain -> DomTable -> Maybe Odin.Domain | |
longestSuffix (Odin.Domain labels) table = case go (reverse labels) table of | |
[] -> Nothing | |
xs -> Just (Odin.Domain (reverse xs)) | |
where | |
go [] _ = [] | |
go (x:xs) (DomTable table) = case M.lookup x table of | |
Just sub -> x : go xs sub | |
Nothing -> [] | |
emptyDomTable :: DomTable | |
emptyDomTable = DomTable M.empty | |
insert :: Odin.Domain -> DomTable -> DomTable | |
insert (Odin.Domain labels) table = go (reverse labels) table | |
where | |
go [] table = table | |
go (x:xs) (DomTable table) | |
| Just sub <- M.lookup x table | |
= DomTable (M.insert x (go xs sub) table) | |
| otherwise | |
= let chain = foldr (\x t -> DomTable (M.singleton x t)) (DomTable M.empty) xs | |
in DomTable (M.insert x chain table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment