You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
660 B
28 lines
660 B
#! /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
|
|
|