Browse Source

suffix trees in haskell

pull/1/head
wes 7 years ago
parent
commit
220bc3db07
  1. 28
      suffixes.hs

28
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
Loading…
Cancel
Save