fork(2) download
  1. -- Lexicographical trie
  2. -- (c) Vadim Vinnik, 2011
  3.  
  4. import Char
  5.  
  6. data TrieNode = TrieNode Char Bool Trie
  7. deriving Show
  8.  
  9. type Trie = [TrieNode]
  10.  
  11. --
  12. trieInsert :: Trie -> String -> Trie
  13.  
  14. trieInsert [] (char:[]) = [TrieNode char True []]
  15.  
  16. trieInsert [] (char:wordRest) = [TrieNode char False (trieInsert [] wordRest)]
  17.  
  18. trieInsert (node@(TrieNode char1 bFinal trieChildren):trieSiblings) word@(char2:wordRest)
  19. | char1 == char2 && wordRest == [] = (TrieNode char1 True trieChildren):trieSiblings
  20. | char1 == char2 = (TrieNode char1 bFinal (trieInsert trieChildren wordRest)):trieSiblings
  21. | char1 > char2 = (trieInsert [] word) ++ (node : trieSiblings)
  22. | otherwise = node : (trieInsert trieSiblings word)
  23.  
  24. --
  25. trieExists :: Trie -> String -> Bool
  26.  
  27. trieExists [] _ = False
  28.  
  29. trieExists ((TrieNode char1 bFinal trieChildren):trieSiblings) word@(char2:wordRest)
  30. | char1 == char2 && wordRest == [] = bFinal
  31. | char1 == char2 = trieExists trieChildren wordRest
  32. | otherwise = trieExists trieSiblings word
  33.  
  34. --
  35. trieListAll :: Trie -> [String]
  36.  
  37. trieListAll [] = []
  38.  
  39. trieListAll ((TrieNode char bFinal trieChildren):trieSiblings) =
  40. (if bFinal then [[char]] else []) ++
  41. (map (char:) (trieListAll trieChildren)) ++
  42. (trieListAll trieSiblings)
  43.  
  44. --
  45. trieEndings :: Trie -> String -> [String]
  46.  
  47. trieEndings [] _ = []
  48.  
  49. trieEndings trie [] = trieListAll trie
  50.  
  51. trieEndings (node@(TrieNode char1 bFinal trieChildren):trieSiblings) word@(char2:wordRest)
  52. | char1 == char2 = (if bFinal then [""] else []) ++ trieEndings trieChildren wordRest
  53. | otherwise = trieEndings trieSiblings word
  54.  
  55. --
  56. trieCompletions :: Trie -> String -> [String]
  57.  
  58. trieCompletions trie word = map (word++) (trieEndings trie word)
  59.  
  60. --
  61. trieFromText :: String -> Trie
  62.  
  63. trieFromText string = foldl trieInsert [] (words string)
  64.  
  65.  
  66. --
  67. main = do s <- getLine
  68. let t = map (\c -> (if isAlpha c then c else ' ')) s
  69. let r = trieFromText t
  70. print (trieEndings r "wh")
  71. print (trieCompletions r "wh")
  72. print (trieEndings r "th")
  73. print (trieCompletions r "th")
  74.  
stdin
War and Peace is generally thought to be one of the greatest novels ever written, remarkable for its dramatic breadth and unity. Its vast canvas includes 580 characters, many historical with others fictional. The story moves from family life to the headquarters of Napoleon, from the court of Alexander I of Russia to the battlefields of Austerlitz and Borodino. Tolstoy's original idea for the novel was to investigate the causes of the Decembrist revolt, to which it refers only in the last chapters, from which can be deduced that Andrei Bolkonski's son will become one of the Decembrists. The novel explores Tolstoy's theory of history, and in particular the insignificance of individuals such as Napoleon and Alexander. Somewhat surprisingly, Tolstoy did not consider War and Peace to be a novel (nor did he consider many of the great Russian fictions written at that time to be novels). This view becomes less surprising if one considers that Tolstoy was a novelist of the realist school who considered the novel to be a framework for the examination of social and political issues in nineteenth-century life.[14]War and Peace (which is to Tolstoy really an epic in prose) therefore did not qualify. Tolstoy thought that Anna Karenina was his first true novel
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )
Linking prog ...
stdout
["ich","o"]
["which","who"]
["at","e","eory","erefore","ought"]
["that","the","theory","therefore","thought"]