Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/f.hs Secret

Created December 20, 2021 17:57
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/41d18409f95b98963df7bdff681ecdbd to your computer and use it in GitHub Desktop.
Save dminuoso/41d18409f95b98963df7bdff681ecdbd to your computer and use it in GitHub Desktop.
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