From 220bc3db07745f9e718720d7a2590aeabc175d9b Mon Sep 17 00:00:00 2001 From: wes Date: Sun, 2 Jul 2017 23:59:57 -0400 Subject: [PATCH] suffix trees in haskell --- suffixes.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 suffixes.hs diff --git a/suffixes.hs b/suffixes.hs new file mode 100644 index 0000000..42386dc --- /dev/null +++ b/suffixes.hs @@ -0,0 +1,28 @@ +#! /usr/bin/env runghc + +import qualified Data.List as L +import qualified Data.Function as F +import Control.Monad + +data Trie = TBranch { + getRoot :: String, + getChildren :: [Trie] + } + deriving (Show) + +notEmpty [] = False +notEmpty _ = True + +buildTrie [] = TBranch "" [] +buildTrie words = + let root = head $ head words + groups = groupTails $ map tail words + in TBranch [root] $ map buildTrie groups + +trie words = TBranch "" (map buildTrie $ groupTails $ tails words) + +groupTails [] = [] +groupTails xs = L.groupBy ((==) `F.on` head) $ L.sort $ filter notEmpty xs + +tails "" = [] +tails (w@(c:cs)) = w : tails cs